Skip to content

Shared memory

Waiting for something to be true

Condition variables, and why waking up is not a promise.

14 minutes · condition variable · spurious wakeup · predicate loops

Being woken is not being served

A condition variable lets a thread give up the lock and sleep until somebody says the thing it wants might now be true. The word doing the work is *might*.

Waking is not a handover. The waiter still has to reacquire the lock, and between the signal and the reacquisition anything can happen — including another thread walking in and taking the very item the waiter was woken for. Which is why the check belongs in a loop, and why every textbook says so in a sentence that never quite explains itself.

Everything you do here stays in this browser.