pthread_cond_wait(condition,mutex)
pthread_cond_signal(condition)
pthread_cond_broadcast(condition)
pthread_cond_wait()
blocks the calling thread until the specified condition is signalled. This routine should be called while mutex is locked, and it will automatically release the mutex while it waits. After signal is received and thread is awakened, mutex will be automatically locked for use by the thread. The programmer is then responsible for unlocking mutex when the thread is finished with it.Recommendation: Using a WHILE loop instead of an IF statement (see watch_count routine in example below) to check the waited for condition can help deal with several potential problems, such as:
pthread_cond_signal()
routine is used to signal (or wake up) another thread which is waiting on the condition variable. It should be called after mutex is locked, and must unlock mutex in order for pthread_cond_wait()
routine to complete.pthread_cond_broadcast()
routine should be used instead of pthread_cond_signal()
if more than one thread is in a blocking wait state.pthread_cond_signal()
before calling pthread_cond_wait()
.pthread_cond_wait()
may cause it NOT to block.pthread_cond_signal()
may not allow a matching pthread_cond_wait()
routine to complete (it will remain blocked).