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);
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 locked.
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 now-
sleeping thread might care about. A typical usage looks like this:
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_mutex_lock(&lock); while (ready == 0) { pthread_cond_wait(&cond, &wait); } pthread_mutex_unlock(&lock);
This is a placeholder.
Operating System: Three Easy Pieces --- Condition Variables (Note)
原文:http://www.cnblogs.com/miaoyong/p/4977316.html