linuxpthreadsmutex

print odd and even numbers using 2 threads using mutex in C


Two threads in the program alternately print even and odd numbers till 100. I have tried this and it worked. Is there a way to access the value of the shared data inside main and terminate the 2 threads when the value reaches 100

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

pthread_t tid[2];
unsigned int shared_data = 0;
pthread_mutex_t mutex;
unsigned int rc;
//prototypes for callback functions

void* PrintEvenNos(void*);
void* PrintOddNos(void*);

void main(void)
{
    pthread_create(&tid[0],0,&PrintEvenNos,0)
    pthread_create(&tid[1],0,&PrintOddNos,0);
    sleep(3);

    pthread_join(tid[0],NULL);
    pthread_join(tid[1],NULL);
}

void* PrintEvenNos(void *ptr)
{
    pthread_mutex_lock(&mutex);
    do
    {
       if(shared_data%2 == 0)
       {
         printf("Even:%d\n",shared_data);
         shared_data++;
       } else {
          rc=pthread_mutex_unlock(&mutex);//if number is odd, do not print, release mutex
       }
    } while(shared_data <= 100);
}

void* PrintOddNos(void* ptr1)
{
    rc = pthread_mutex_lock(&mutex);
    do
    {
       if(shared_data%2 != 0)
       {
          printf("odd:%d\n",shared_data);
          shared_data++;
       } else {
          rc = pthread_mutex_unlock(&mutex);//if number is even, do not print, release mutex
       }
    } while(shared_data <= 100);
}

Solution

  • There were few errors, in the code I posted earlier, I corrected those mistakes and this program prints even and odd values from 0 to 100, I have tried it.

    #include<stdio.h>
    #include<pthread.h>
    
    pthread_t tid[2];
    unsigned int shared_data = 0;
    pthread_mutex_t mutex;
    unsigned int rc;
    //prototypes for callback functions
    
    void* PrintEvenNos(void*);
    void* PrintOddNos(void*);
    
    void main(void)
    {
        pthread_create(&tid[0],0,&PrintEvenNos,0);
        pthread_create(&tid[1],0,&PrintOddNos,0);
        sleep(3);
    
        pthread_join(tid[0],NULL);
        pthread_join(tid[1],NULL);
    }
    
    void* PrintEvenNos(void *ptr)
    {
         rc = pthread_mutex_lock(&mutex);
         do
         {
             if(shared_data%2 == 0)
             {
                 printf("Even:%d\n",shared_data);
                 shared_data++;
             }
             else
             {
                 rc=pthread_mutex_unlock(&mutex);//if number is odd, do not print, release mutex
             }
         } while (shared_data <= 100);
    }
    
    void* PrintOddNos(void* ptr1)
    {
        rc = pthread_mutex_lock(&mutex);
        do
        {
            if(shared_data%2 != 0)
            {
                printf("odd:%d\n",shared_data);
                shared_data++;
            }
            else
            {
                rc = pthread_mutex_unlock(&mutex);//if number is even, do not print, release mutex
            }
        } while (shared_data <= 100);
    }