In this article, we will see how to synchronize two threads using the mutex and conditional variables.

We will use the following API's from the pthread library

We will be writing a simple example program that has two threads in which one thread prints an even number and the other thread prints an odd number in a synchronization manner.

#include <stdio.h>

int counter = 1;

void* odd() {

while (counter <= 10) {

if (counter % 2 == 0) {
}
else {
printf("odd\n");
printf("%d\n", counter);
counter = counter + 1;
}

}
}

void* even(void* arg) {

while (counter <= 10) {

if (counter % 2 != 0) {
}

else {
printf("even\n");
printf("%d\n", counter);
counter = counter + 1;
}

}

}

int main()
{

return 0;
}

### Output

$gcc -pthread -o thread thread.c$ ./thread

odd
1
even
2
odd
3
even
4
odd
5
even
6
odd
7
even
8
odd
9
even
10

### Explanation

While creating a thread we have to pass the thread attributes which includes scheduling policy, scheduling priority, stack size, guard size e.t.c. All of these attributes can be initialized by using the pthread_attr function.

After initializing the thread we use the pthread_create function to create the thread.

When the main process terminates, automatically the threads associated with it will also get terminated. So, to prevent that we used the pthread_exit() function which prevents the main process to wait for the thread termination.

The logic which we implemented is pretty straightforward. After two threads are created either of the thread will get scheduled.

For example, if an even thread gets scheduled at first then it will enter into the if condition because initially, the counter has an odd value i.e 1 and it will be kept into wait state by the pthread_cond_wait(odd_cond) and mutex will be released.

As the even thread is in a wait state, now, the odd thread will get scheduled and it will enter into the else block and lock the mutex and then increment the counter to 2 (even). After that, it will give a signal to unblock the even thread using pthread_signal() and then it will enter into a wait state.

Likewise, after the even thread completes its task then it will enter into a wait state and give a signal to the odd thread and the same thing continues till the end.