Hi ,
Below code prints odd and even numbers using two threads.code works correctly .
But i am unable to figure out whether it works in all scenarios.

My doubtful scenario is:
Lets say evenfun thread is invoked first when the value of count is 2 , so the thread waits and it also acquired the lock.
as evenfun thread waits oddfun thread starts to execute and try to acquire the lock.

So here , oddfun should not get the lock because the mutex is already locked by evenfun, but this is not happening at all.

Please let me know whether my understanding is wrong , or is it because of some mutex properties.

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

#define MAX 20
int count =0;

void *evenfun(void *x);
void *oddfun(void *x);

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int main()
{
    pthread_t o_t, e_t;

    pthread_create(&o_t, NULL,oddfun, NULL);
    pthread_create(&e_t, NULL,evenfun, NULL);

    pthread_join(o_t,NULL);
    pthread_join(e_t,NULL);

    return 0;

}

void *evenfun(void *x)
{

    while(1) {
        pthread_mutex_lock(&mutex);
    if(count%2 == 0)
    {
        printf("count value %d, waiting in even...\n", count);
        pthread_cond_wait(&cond,&mutex);
    }
    count++;

    if( count > MAX)
    {
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
        return NULL;
    }
    printf("even: %d \n", count);
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);
    }
}

void *oddfun(void *x)
{

    while (1){
    pthread_mutex_lock(&mutex);
    if(count%2 != 0)
    {
        printf("count value %d, waiting in odd...", count);
        pthread_cond_wait(&cond,&mutex);
    }
    count++;

    if( count > MAX)
    {
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
        return NULL;
    }
    printf("odd: %d \n", count);
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);
    }
}

Hi,
Just want to know if a thread is waiting, by calling pthread_cond_wait(), does it unlock the mutex and similarly if a thread is signalling by calling pthread_cond_signal() does it lock the associated mutex.

i found below descriptions , but because of my poor english , i dont get the real meaning.
http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_cond_wait.html
http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_cond_signal.html

The pthread_cond_wait() and pthread_cond_timedwait() functions are used to block on a condition variable. They are called with mutex locked by the calling thread or undefined behaviour will result.
These functions atomically release mutex and cause the calling thread to block on the condition variable cond; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". That is, if another thread is able to acquire the mutex after the about-to-block thread has released it, then a subsequent call to pthread_cond_signal() or pthread_cond_broadcast() in that thread behaves as if it were issued after the about-to-block thread has blocked.

The pthread_cond_signal() or pthread_cond_broadcast() functions may be called by a thread whether or not it currently owns the mutex that threads calling pthread_cond_wait() or pthread_cond_timedwait() have associated with the condition variable during their waits; however, if predictable scheduling behaviour is required, then that mutex is locked by the thread calling pthread_cond_signal() or pthread_cond_broadcast().

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.