Condition variables
A condition variable lets a thread sleep until another thread announces that a condition might be true. In C++ you wait on a std::unique_lock and a predicate; you wake waiters with notify_one or notify_all.
Detailed Explanation
A mutex alone can only say "do not touch this now". A condition variable says "wake me when the queue is not empty". The waiter locks the mutex, checks the condition, and if it is false calls cv.wait(lock, predicate). wait unlocks the mutex, sleeps, then relocks and rechecks the predicate. The notifier sets the condition under the same mutex and calls notify_one or notify_all.
The predicate is mandatory in practice. wait can return spuriously — the standard allows a wake with no notify. The predicate loop is while (!ready) cv.wait(lock);, which the two-argument wait writes for you.
The mutex, the condition variable, and the shared data are a triple. The notifier must change the data before notify, and must do it while holding the mutex (or you need a more careful atomic protocol). If you notify without the mutex, a waiter can check the condition, see false, and be about to wait just as you notify: the notify is lost and the waiter sleeps forever. Holding the mutex for the write-plus-notify closes that window for the usual pattern.
notify_one wakes one waiter. notify_all wakes all. Use notify_all when any waiter might be able to proceed, or when the predicate differs per waiter. Use notify_one for a single-consumer queue.
wait_for and wait_until time out. They still need a predicate: a timeout is not "the condition is true".
std::condition_variable works only with unique_lock<mutex>. std::condition_variable_any works with any lockable type, including a stop_token wait in C++20 (wait(lock, stop_token, pred)).
Do not use a condition variable as a semaphore you notify before anyone waits without also setting a persistent flag. The flag is the condition.
Spurious wakes plus missed notifies are the two ways people invent "condition variables are broken". They are not. The predicate and the mutex protocol are the API.
Code Examples
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <queue>
#include <thread>
std::mutex mu;
std::condition_variable cv;
std::queue<int> jobs;
void consumer() {
std::unique_lock<std::mutex> lock(mu);
cv.wait(lock, [] { return !jobs.empty(); });
std::cout << jobs.front() << '\n';
jobs.pop();
}
int main() {
std::thread t(consumer);
{
std::lock_guard<std::mutex> lock(mu);
jobs.push(42);
}
cv.notify_one();
t.join(); // prints: 42
}Common Gotchas
Waiting without a predicate
a spurious wake proceeds as if the event happened. Always pass a predicate or write the `while` loop.
Notify without setting a persistent condition
if the waiter has not reached `wait` yet, the notify is gone. Set `ready = true` under the mutex.
`condition_variable` and `shared_mutex` / other mutexes
you need `condition_variable_any`.
`wait` unlocks, so other threads can run
the predicate is re-evaluated after relock. Do not assume the world is unchanged except for your condition.