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.
If a thread sends a signal to wake up all threads waiting
on a condition variable and there are no threads waiting on that condition
variable, the signal is discarded and no action is taken.
Note: It is possible
for one thread to signal a condition immediately before another thread begins
waiting on that condition variable, resulting in no 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 the thread up.