Multithreaded Programming Guide

Exit Print View

Updated: July 2014
 
 

Condition Variable Attributes

Use condition variables to atomically block threads until a particular condition is true. Always use condition variables together with a mutex lock.

With a condition variable, a thread can atomically block until a condition is satisfied. The condition is tested under the protection of a mutual exclusion lock (mutex).

When the condition is false, a thread usually blocks on a condition variable and atomically releases the mutex waiting for the condition to change. When another thread changes the condition, that thread can signal the associated condition variable to cause one or more waiting threads to perform the following actions:

  • Wake up

  • Acquire the mutex again

  • Re-evaluate the condition

Condition variables can be used to synchronize threads among processes in the following situations:

  • The threads are allocated in memory that can be written to

  • The memory is shared by the cooperating processes

The scheduling policy determines how blocking threads are awakened. The default scheduling policy, SCHED_OTHER, does not specify the order in which threads are awakened. Under the SCHED_FIFO and SCHED_RR real-time scheduling policies, threads are awakened in priority order.

The attributes for condition variables must be set and initialized before the condition variables can be used. The functions that manipulate condition variable attributes are listed in Table 4–4.

Table 4-4  Condition Variable Attributes
Operation
Function Description
Initialize a condition variable attribute
Remove a condition variable attribute
Set the scope of a condition variable
Get the scope of a condition variable
Get the clock selection condition variable attribute
Set the clock selection condition variable attribute