Print even and odd numbers using two threads using mutex in C

In this blog post, you will learn how to print even and odd numbers in synchronize order using the two threads and mutex in C.

Primary prerequisite:

 

In the below code one thread will print all even numbers and the other all odd numbers. In this code, we will use the mutex to synchronize the output in sequence ie. 0,1,2,3,4….etc.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

int MAX = 100;
volatile int count = 0;

pthread_mutex_t mutex;
pthread_cond_t cond;

void *printEvenNum(void *arg)
{
    while(count < MAX)
    {
        pthread_mutex_lock(&mutex);
        while(count % 2 != 0)
        {
            pthread_cond_wait(&cond, &mutex);
        }
        printf("%d ", count++);
        pthread_mutex_unlock(&mutex);
        pthread_cond_signal(&cond);
    }
    pthread_exit(0);
}

void *printOddNum(void *arg)
{
    while(count < MAX)
    {
        pthread_mutex_lock(&mutex);
        while(count % 2 != 1)
        {
            pthread_cond_wait(&cond, &mutex);
        }
        printf("%d ", count++);
        pthread_mutex_unlock(&mutex);
        pthread_cond_signal(&cond);
    }
    pthread_exit(0);
}

int main()
{
    pthread_t thread1;
    pthread_t thread2;

    pthread_mutex_init(&mutex, 0);
    pthread_cond_init(&cond, 0);

    pthread_create(&thread1, 0, &printEvenNum, NULL);
    pthread_create(&thread2, 0, &printOddNum, NULL);

    pthread_join(thread1, 0);
    pthread_join(thread2, 0);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);

    return  0;
}

Output:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 
95 96 97 98 99 100

Now you are thinking what is the use of mutex in this code.

I can understand your question, in the beginning, most new learners are not aware of the power of mutex. Let’s understand what happens if we do not use mutex in our code.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

int MAX = 100;
int count = 0;

void *printEvenNum(void *arg)
{
    while(count < MAX)
    {
        if(count % 2 == 0)
        {
            printf("%d ", count++);
        }
    }
    pthread_exit(0);
}

void *printOddNum(void *arg)
{
 
    while(count < MAX)
    {
        if(count % 2 == 1)
        {
            printf("%d ", count++);
        }
    }
    pthread_exit(0);
}

int main()
{
    pthread_t t1;
    pthread_t t2;

    pthread_create(&t1, 0, &printEvenNum, NULL);
    pthread_create(&t2, 0, &printOddNum, NULL);

    pthread_join(t1, 0);
    pthread_join(t2, 0);

    return  0;
}

Output:

0 2 1 3 4 5 6 7 9 8 10 12 11 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 29 28 30
32 31 33 34 36 35 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91 93 92 94 96 95 97 98 99

You can see without mutex both threads are not printing in synchronize order.

 

Recommended Post

Leave a Reply

Your email address will not be published. Required fields are marked *