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.
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,queue_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,queue_mutex);
do pop...
signal(queue_is_not_full_cond)
unlock(queue_mutex)
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.
精彩评论