Lesson 11: Condition Variables — Thread Coordination
A mutex prevents race conditions, but does not let a thread wait until a condition becomes true. Condition variables let a thread sleep efficiently until another thread signals that something has changed.
A condition variable is like a shop bell: you wait at the door and when someone puts an item on the shelf they ring — then you enter.
- condition variable
- A synchronisation primitive that lets a thread atomically wait for a condition while releasing the mutex, and wake up when another thread signals.
- producer-consumer
- A programming pattern where one thread (producer) generates data and places it in a buffer, and another thread (consumer) takes and processes it.
- spurious wakeup
- A situation where pthread_cond_wait returns even though no thread called signal. Requires checking the predicate in a loop.
- broadcast
- pthread_cond_broadcast wakes all threads waiting on a condition variable, unlike signal which wakes only one.
- predicate
- The condition checked before and after pthread_cond_wait to guard against spurious wakeups and TOCTOU issues.