views:

31

answers:

2

I went through the documentation in http://www.opengroup.org/onlinepubs/009695399/functions/pthread_cond_wait.html but this is not mentioned explicitly. Any prompt response will be very appreciated.

+2  A: 

Yes. This is sometimes a good idea if you have separate conditions you'd like to wait on. For instance, you might have a queue and condition variables for both "not full" and "not empty" etc... Someone putting data on the queue waits for "not full". Someone taking data off the queue waits for "not empty". They all use the same mutex.

xscott
@xscott Thanks a lot. Any relevant links?
Fanatic23
Google turns up this example: http://cs.wmich.edu/~jjwillia/10.spring/cs4540/asgn2/pthreads-pro-con
xscott
+2  A: 

Yes. This is common pratice:

Typical example:

mutex queue_mutex; 
cond queue_is_not_full_cond;
cond queue_is_not_empty_cond;

push() 
   lock(queue_mutex)
      while(queue is full)
        wait(queue_is_not_full_cond,mutex);
      do push...
      signal(queue_is_not_empty_cond)
   unlock(queue_mutex)

pop() 
   lock(queue_mutex)
      while(queue is empty)
        wait(queue_is_not_empty_cond,mutex);
      do pop...
      signal(queue_is_not_full_cond)
   unlock(queue_mutex)
Artyom
@Artyom +1 for the pseudo code
Fanatic23