Condition variables and threads

Condition variables allow threads to wait for certain events or conditions to occur and they notify other threads that are also waiting for the same events or conditions.

The thread can wait on a condition variable and broadcast a condition such that one or all of the threads that are waiting on the condition variable become active. You can consider condition variables to be similar to using events to synchronize threads on other platforms.

Condition variables do not have ownership associated with them and are typically stateless. A stateless condition variable means that if a thread signals a condition variable to wake up a waiting thread when there are currently no waiting threads, the signal is discarded and no action is taken. The signal is effectively lost. It is possible for one thread to signal a condition immediately before a different thread begins waiting for it without any resulting action.

Locking protocols that use mutual exclusions (mutexes) are typically used with condition variables. If you use locking protocols, your application can ensure that a thread does not lose a signal that was intended to wake it up.