Hi ,
Below is the code for printing even/odd numbers alternatively.But some how i am not able to achieve the result.
I have seen a version with while loop http://www.bogotobogo.com/cplusplus/quiz_multithreading.php working perfect.
But i would like to use one thread for printing one value instead of same thread running in loop as shown in example.

I hope my main function is correct, just by modifying my thread functions can i achieve my desired out put.

please help.

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

void* OddFunc(void *p);
void* EvenFunc(void *p);

int g_var = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_v = PTHREAD_COND_INITIALIZER;

#define NOOFTHRDS 10

int main()
{
    pthread_t o_tid[NOOFTHRDS],e_tid[NOOFTHRDS];
    pthread_attr_t attr;
    int i ;
    void *status;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    for( i= 0;i<NOOFTHRDS;i++) {
        pthread_create(&o_tid[i],NULL,OddFunc,&i);
        pthread_create(&e_tid[i],NULL,EvenFunc,&i);

    }

    for( i= 0;i<NOOFTHRDS;i++) {
        pthread_join(e_tid[i],&status);
        pthread_join(o_tid[i],&status);
    }

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond_v);
//  pthread_exit(NULL);
    return 0;

}

void* OddFunc(void *p)
{
    if(g_var%2 != 1) {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond_v, &mutex);
        printf("I= %d , O: %d\n",*(int *)p, g_var++);
    }
    else{
        pthread_mutex_lock(&mutex);
        printf("I= %d , O: %d\n",*(int *)p, g_var++);
        pthread_mutex_unlock(&mutex);
        pthread_cond_signal(&cond_v);
    }
    pthread_exit(NULL);
}
void* EvenFunc(void *p)
{
       if(g_var%2 != 0){
            pthread_mutex_lock(&mutex);
                pthread_cond_wait(&cond_v, &mutex);
                printf("I= %d , E: %d\n", *(int *)p,g_var++);
        }
      else {
               pthread_mutex_lock(&mutex);
           printf("I= %d , E: %d\n", *(int *)p,g_var++);
           pthread_mutex_unlock(&mutex);
               pthread_cond_signal(&cond_v);

    }
       pthread_exit(NULL);
}

Recommended Answers

All 3 Replies

You cannot determine the order that threads will run in (scheduled), or how much time they will be allocated. To accomplish what you want, each thread will have to syncronize with a mutex, basically turning your code into a single thread. Why do you want to do this?

And yes, I see that you are using a mutex, but your mistake here is that you don't lock the mutex on entry to your odd/even functions, even though it is effectively what you are trying to do in your code. IE, stop trying to be clever. Remember the KISS principle!

I am only trying to understand how the sychronization works, i have implemented the code,as per my understanding, after reading descriptions. But i am not expert.
i have seen the working version, but it cannot be only one , there may be different ways we can implement same, so i tried with if/else version but its not working.

can you please highlight what exactly i am missing; and provide some pseudo.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.