Condition variable synchronization APIs
Condition variables are synchronization objects that allow threads to wait for certain events (conditions) to occur. Condition variables are slightly more complex than mutexes, and the correct use of condition variables requires the thread to co-operatively use a specific protocol in order to ensure safe and consistent serialization. The protocol for using condition variables includes a mutex, a boolean predicate (true/false expression) and the condition variable itself. The threads that are cooperating using condition variables can wait for a condition to occur, or can wake up other threads that are waiting for a condition.
The table below lists important conditional variables attributes, their default values, and all supported values.
Attribute |
Default value |
Supported values |
pshared |
PTHREAD_PROCESS_PRIVATE |
PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED |
For information about the examples included with the APIs, see the information on the API examples.
The Condition variable synchronization APIs are:
- pthread_cond_broadcast() (Broadcast Condition to All Waiting Threads) wakes up all threads that are currently waiting on the condition variable specified by cond.
- pthread_cond_destroy() (Destroy Condition Variable) destroys the condition variable specified by cond.
- pthread_cond_init() (Initialize Condition Variable) initializes a condition variable object with the specified attributes for use.
- pthread_cond_signal() (Signal Condition to One Waiting Thread) wakes up at least one thread that is currently waiting on the condition variable specified by cond.
- pthread_cond_timedwait() (Timed Wait for Condition) blocks the calling thread, waiting for the condition specified by cond to be signaled or broadcast to.
- pthread_cond_wait() (Wait for Condition) blocks the calling thread, waiting for the condition specified by cond to be signaled or broadcast to.
- pthread_condattr_destroy() (Destroy Condition Variable Attributes Object) destroys the condition variable attributes object specified by attr, and indicates that any storage that the system has associated with the object be de-allocated.
- pthread_condattr_getpshared() (Get Process Shared Attribute from Condition Attributes Object) retrieves the current setting of the process shared attribute from the condition attributes object.
- pthread_condattr_init() (Initialize Condition Variable Attributes Object) initializes the condition variable attributes object specified by attr to the default attributes.
- pthread_condattr_setpshared() (Set Process Shared Attribute in Condition Attributes Object) sets the current pshared attribute for the condition attributes object.
- pthread_get_expiration_np() (Get Condition Expiration Time from Relative Time) computes an absolute time by adding the specified relative time (delta) to the current system time.