Condition Variables
This lesson discusses condition variables, the machinery to enable signaling between threads.
We'll cover the following...
The other major component of any threads library, and certainly the case with POSIX threads, is the presence of a condition variable. Condition variables are useful when some kind of signaling must take place between threads if one thread is waiting for another to do something before it can continue. Two primary routines are used by programs wishing to interact in this way:
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);int pthread_cond_signal(pthread_cond_t *cond);
Usage
To use a condition variable, one has to in addition have a lock that is associated with this condition. When calling either of the above routines, this lock should be held.
The first routine, pthread_cond_wait()
, puts the calling thread to sleep and thus waits for some other thread to signal it, usually when something in the program has changed that the ...