Unformatted text preview: ed to as releasing the mutex. At any point in time, at most one thread can hold the mutex. 11.5.2 Condition Variables
Condition variables are synchronization variables that are used for signaling. Pthreads denes three basic operations on condition variables.
#include <pthread.h> int pthread cond init(pthread cond t *cond, pthread condattr t *attr); int pthread cond wait(pthread cond t *cond, pthread mutex t *mutex); int pthread cond signal(pthread cond t *cond);
return: 0 if OK, nonzero on error 11.5. SYNCHRONIZING THREADS WITH MUTEX AND CONDITION VARIABLES 587 A condition variable cond must be initialized before it is used, either by calling pthread cond init or at compile-time:
1 pthread_cond_t cond = PTHREAD_COND_INITIALIZER; For our purposes the attr argument will always be NULL and can be safely ignored. A thread waits for some program condition associated with the condition variable cond to become true by calling pthread cond wait. In order to guarantee that the call to pthread cond wait is indivisible with respect to other instances of pthread cond wait and pthread cond signal, Pthreads requires that a mutex variable mutex be associated with the condition variable cond, and...
View Full Document