I need to print 2 messages each one in each thread in C and synchronize them.

The one thread prints One and the second prints Two.

So my code is something like that

void printOne(void* empty){
    while(1) printf("One ");  
}

void printTwo(void* empty){
    while(1) printf("Two\n");
}


int main(){

    pthread_t t_1,t_2;

    pthread_create(&t_1, NULL,(void *)&printOne,NULL);
    pthread_create(&t_2, NULL,(void *)&printTwo,NULL);

    pthread_join(t_1,NULL);
    pthread_join(t_2,NULL);

    exit(0);
}

The problem is that randomly prints One and Two but not always in that sequence. I would like to make it print always Two after One. I got a bit confused with the join command.

Thanks in advance!

有帮助吗?

解决方案

You are confusing some basic concepts about synchronization here. The pthread_join() function will not guarantee synchronization in the way you are thinking. The join is used to synchronize threads after their execution have finished, i.e., after return is called from within your thread. This way, the calling thread will wait for the specified thread to finish its execution, which is exactly what your code is doing. The main thread is waiting for:

  1. First, t_1 to end
  2. Then, t_2 to end

If t_2 ends before t_1, the main thread will still be blocked by t_1, because this order has to be respected. Of course none of them will finish their execution in your code, since both are stuck in an infinite loop (while(1)).

What you are trying to achieve can be done using many techniques. I'd suggest you to use semaphores (if you want to use the POSIX API) or mutex (already implemented in pthread library).

Here's an example of how your code can be changed to get some synchronization:

void printOne(void* empty){
    while(1)
    { 
      sem_wait(&s1); //wait for semaphore s1
      printf("One ");  
      sem_post(&s2); //signal semaphore s2
    }
}

void printTwo(void* empty){
    while(1)
    {
      sem_wait(&s2); //wait for semaphore s2
      printf("Two\n");
      sem_post(&s1); //signal semaphore s1
    }
}

sem_t s1, s2; //Declare the semaphores globally, so the threads can access them

int main(){

    pthread_t t_1,t_2;

    sem_init(&s1, 0, 1); //Initialize s1 with 1
    sem_init(&s2, 0, 0); //Initialize s2 with 0

    pthread_create(&t_1, NULL,(void *)&printOne,NULL);
    pthread_create(&t_2, NULL,(void *)&printTwo,NULL);

    pthread_join(t_1,NULL);
    pthread_join(t_2,NULL);

    exit(0);
}

This way, your code guarantees that one message after another are going to be printed to your output:

One
Two
One
Two
...

其他提示

Why use threads at all if you want it to be synchronous? Just print them sequentially in main().

Otherwise... I guess you could just run one thread after the other. pthread_join makes the program wait for the thread to finish before continuing.

int main(){

    pthread_t t_1,t_2;

    pthread_create(&t_1, NULL,(void *)&printOne,NULL);
    pthread_join(t_1,NULL);

    pthread_create(&t_2, NULL,(void *)&printTwo,NULL);
    pthread_join(t_2,NULL);

    exit(0);
}

You will have to make a breaking condition in your printOne and printTwo functions if you want the threads to actually finish, though...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top