Thursday 26 September 2013

Pthread_cond_wait & Pthread_cond_signal simple example

3 comments
pthread_cond_t
pthread_cond_t is a condition variable which can be used by thread_cond_wait(), pthread_cond_signal(), pthread_cond_broadcast() and thread_cond_timedwait().

pthread_cond_wait() :
  • This routine stops the execution of the calling thread and wait for appropriate signal to start the thread execution.
  • pthread_cond_wait() must be called after pthread_mutex_lock() and before pthread_mutex_unlock().
  • pthread_cond_wait() release the mutex lock while it is waiting, so that pthread_cond_signal(), which is also called in the mutex should get access and can give signal to waiting thread to awake.
Syntax: pthread_cond_wait(pthread_cond_t *condition, pthread_mutex_t *mutex)

pthread_cond_signal() 
  • This routine is used to wakeup the thread which is waiting for any condition to occur.
  • If in any case pthread_cond_signal() calls first before the execution of pthread_cond_wait() then it cannot awake the thread which is waiting for the condition to occur and your thread calling pthread_cond_wait() will go to the infinite waiting state or can be called as deadlock state.
  • Your pthread_cond_signal() should be called at least once for corresponding pthread_cond_wait() function call.
Syntax: pthread_cond_signal(pthread_cond_t *condition)
This signal will wakeup the pthread_cond_wait() with condition variable condition.
The program given below will explain this routines signal_wait.c


#include "pthread.h"
#include "stdio.h"
#include"unistd.h"
 
volatile int sv=10;
volatile int x,y,temp=10;
pthread_mutex_t mut=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t con=PTHREAD_COND_INITIALIZER;
pthread_t child1,child2,child3;
int condition=1,val=1;
int count=1;
 
void *ChildThread1(void *arg)
{
    sleep(1);
    pthread_mutex_lock (&mut);
        printf("wait called\n");
        while(count<10)                      //if while loop with signal complete first don't wait
        {
            printf("In wait\n");
            pthread_cond_wait(&con, &mut);  //wait for the signal with con as condition variable
        }
        x=sv;
        x++;
        sv=x;
        printf("The child1 sv is %d\n",sv);
    pthread_mutex_unlock (&mut);
}
 
void *ChildThread2(void *arg)
{
 
    while(count<10)
    {
        pthread_mutex_lock (&mut);
            pthread_cond_signal(&con);  //wake up waiting thread with condition variable
                                        //con if it is called before this function
            if(val==1)
            {
                y=sv;
                y--;
                sv=y;
                printf("The child2 sv is %d\n",sv);
                val++;
            }
            count++;
        pthread_mutex_unlock (&mut);
         
    }
    printf("mutex released\n");
    printf("chil2 exit\n");
}
 
int main(void)
{
    int co=1;
    pthread_create(&child1,NULL,ChildThread1,NULL);
    pthread_create(&child2,NULL,ChildThread2,NULL);
    pthread_join(child1,NULL);
    pthread_join(child2,NULL);
 
    pthread_cond_destroy(&con);
    pthread_mutex_destroy(&mut);
    pthread_exit(NULL);
    return 0;
}

3 comments:

  1. As you have mentioned pthread_cond_wait() call internally unlock the mutex.
    then why we need pthread_mutex_unlock() at last line of Child1.
    will it through any error, If not then why ??

    ReplyDelete
    Replies
    1. You can check man page of pthread_cond_wait (man pthread_cond_wait), it says "Upon successful return, the mutex shall have been locked and shall be owned by the calling thread", because of this pthread_mutex_unlock() is must.

      Delete
  2. How can you ensure the first call should be pthread_cond_wait

    ReplyDelete