OSF DCE Application Development Guide--Core Components
Thread Concepts and Operations
Thread B updates the predicate named ready associated with the condition variable to
the state thread A is waiting for. It also executes a signal on the condition variable while
holding the mutex mutex_ready.
Figure 7-5. Thread A Wakes Up and Proceeds
Thread B
var
mutex_var
lock block
Thread A
access
Thread A wakes up, verifies that the condition variable is in the correct state, and
proceeds to execute task Y (see Figure 7-3).
Note that, although the condition variable is used for explicit communications among
threads, the communications are anonymous. Thread B does not necessarily know that
thread A is waiting on the condition variable that thread B signals. And thread A does
not know that it was thread B that woke it up from its wait on the condition variable.
Use the pthread_cond_init() routine to create a condition variable. To create condition
variables as part of the program’s one-time initialization code, see Section 7.5.
Use the pthread_cond_wait() routine to cause a thread to wait until the condition is
signaled or broadcast. This routine specifies a condition variable and a mutex that you
have locked. (If you have not locked the mutex, the results of pthread_cond_wait( ) are
unpredictable.) This routine unlocks the mutex and causes the calling thread to wait on
the condition variable until another thread calls one of the following routines:
• The pthread_cond_signal( ) routine to wake one thread that is waiting on the
condition variable
• The pthread_cond_broadcast() routine to wake all threads that are waiting on a
condition variable
If you want to limit the time that a thread waits for a condition to be signaled or
broadcast, use the pthread_cond_timedwait( ) routine. This routine specifies the
condition variable, mutex, and absolute time at which the wait should expire if the
condition variable is not signaled or broadcast.
You can delete a condition variable and reclaim its storage by calling the
pthread_cond_destroy( ) routine. Use this routine only after the condition variable is no
longer needed by any thread. Condition variables are automatically deleted when the
program terminates.
124245 Tandem Computers Incorporated 7− 11