Condition Variables

How a thread sleeps until shared state changes: the wait protocol, why the wait sits in a while loop, lost and spurious wakeups shown in running code, notify versus notify-all, and Java monitors versus Condition objects.

Overview

A condition variable lets a thread sleep until some shared state becomes true: the buffer is not empty, the job is done, it is my turn. It is always used with a lock. The thread holds the lock, checks the state, and if it has to wait, calls wait(), which releases the lock and puts the thread to sleep in one atomic step. Another thread takes the lock, changes the state, and calls notify to wake a waiter, which re-acquires the lock before wait() returns.

The alternative is polling: loop, check, sleep a little, check again. Polling either burns CPU (short sleeps) or reacts late (long sleeps). A condition variable wakes the waiter when something changed and costs nothing while it sleeps. Set side by side, the three ways to wait look like this.

  • A busy loop on a flag: it uses a full core while waiting and reacts immediately. It needs atomics or volatile to be correct, and it burns power.
  • A loop with sleep: it uses little CPU, but it reacts up to one sleep interval late. It is easy, but always late or wasteful.
  • A condition variable: it uses no CPU while waiting and reacts as soon as a notify happens and the lock is free. The risk is that the wait protocol below must be followed exactly.

The wait protocol

Both sides follow a fixed recipe, and every bug in this area is a step skipped. The waiter checks the state in a loop while holding the lock.

  • Take the lock.
  • Check whether the state is ready.
  • If it is not, call wait(), which releases the lock and sleeps, atomically. When the thread is woken, it re-takes the lock and goes back to step 2.
  • Once the state is ready, act on it and release the lock.

The notifier changes the state under the same lock before it notifies.

  • Take the lock.
  • Change the state.
  • Notify, then release the lock.

Each part of the recipe is a rule, and each rule names what breaks when you skip it.

  • The state lives in ordinary variables, protected by the lock: the condition variable has no memory; it only wakes threads.
  • Check the state before waiting, under the lock: otherwise a notify sent before you wait is lost, and you sleep forever.
  • Wait in a while loop, not an if: otherwise you act on a state that is no longer true (next section).
  • Change the state under the same lock, then notify: otherwise the waiter can check, miss the change, and sleep through the notify.
  • A woken thread re-acquires the lock before wait() returns: there is nothing for you to do here; this is why the loop's re-check is safe.

Python's docs note that notify does not release the lock: the woken thread returns from wait() only when the notifier lets go.

Interview questions

  • What is a condition variable and how does it work?
  • Why must you wait in a while loop instead of an if?
  • What is the difference between notify() and notifyAll(), and which should you use?
  • How do Java monitors (wait/notify) differ from Lock and Condition?
  • Condition variable, semaphore or event: which do you use?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio