Condition variable
A condition variable is a synchronization object that allows threads to wait for a shared-state condition and be notified when it may have changed.
Learn Concurrent Programming with Go
A condition variable is a synchronization mechanism that allows threads or goroutines to wait for a specified condition and be notified when it may hold.
- How it is used
- Coordinates goroutines that must wait for protected shared state to satisfy a condition without timed polling.
- How deep it goes
- Works through Wait, Signal, Broadcast, missed notifications, multi-waiter coordination, and implementations of readers–writer locks and waitgroups.
- Examples
- Uses a bank balance, a multiplayer game lobby, writer starvation, and a custom waitgroup.
- What you can do after
- Build it from scratch as a waitgroup or write-preferring readers–writer lock using a mutex, predicates, waits, and broadcasts.
Read how Learn Concurrent Programming with Go explains condition variable →
Modern C, Third Edition
A condition variable is a thread-synchronization object that allows threads to wait until a shared-state condition may have become true.
- How it is used
- Models conditional processing dependencies between threads with condition variables.
- How deep it goes
- Explains wait loops, timed waits, mutex release and acquisition, and shared-state visibility; examines liveness separately.
- Examples
- Uses accounting, update, and drawing threads with separate conditions and a one-second timed-wait helper.
- What you can do after
- Apply it using C's cnd_t and mutex functions to wait in a loop and recheck shared state.
Read how Modern C, Third Edition explains condition variable →
C++ Concurrency in Action, Second Edition
A condition variable is a synchronization primitive that lets threads wait for a condition and be notified when it may have changed.
- How it is used
- Lets threads wait for work or another condition instead of periodically checking shared data.
- How deep it goes
- Works through mutex-protected queue operations and a thread-safe queue abstraction, without describing implementation internals.
- Examples
- Uses a producer–consumer data queue and a thread-safe queue with push() and wait_and_pop().
- What you can do after
- Build it from scratch by pairing a mutex-protected queue predicate with wait() and notify_one().
Read how C++ Concurrency in Action, Second Edition explains condition variable →
Object-Oriented Software Design in C++
A condition variable is a synchronization primitive on which threads wait for a condition and from which they can be notified when that condition may have changed.
- How it is used
- Synchronizes producer and consumer threads around full and empty states of a shared queue.
- How deep it goes
- Explains unique_lock, wait, notification, condition rechecking, and queue coordination, while deferring broader multithreading complexity.
- Examples
- Uses a bounded SharedQueue with multiple producers, multiple consumers, capacity checks, notifications, and threaded output.
- What you can do after
- Build it from scratch using C++ condition_variable, unique_lock, mutexes, wait loops, and notifications.
Read how Object-Oriented Software Design in C++ explains condition variable →
Software Design for Python Programmers
A condition object is a synchronization object that lets threads wait for a predicate on shared state and be notified when that predicate may have changed.
- How it is used
- Coordinates producer–consumer access to a bounded shared queue when mutexes and semaphores alone are insufficient.
- How deep it goes
- Explains acquisition, predicate loops, release during waiting, reacquisition, rechecking, critical regions, and notification.
- Examples
- Uses a Python bounded queue with multiple producers, multiple consumers, capacity limits, notifications, and zero-value end sentinels.
- What you can do after
- Apply it via Python's threading.Condition to coordinate protected access and waiting threads around shared-state predicates.
Read how Software Design for Python Programmers explains condition variable →
100 Go Mistakes and How to Avoid Them
A condition variable is a synchronization object that allows concurrent executions to wait for and announce the occurrence of a condition.
- How it is used
- Coordinates multiple goroutines waiting for shared state changes without combining the solution with a more complex WaitGroup.
- How deep it goes
- Defines the rendezvous model and traces Wait through unlocking, suspension, notification, and relocking.
- Examples
- Uses donation listeners, balance thresholds, mutex-protected updates, and Broadcast notifications to demonstrate synchronization.
- What you can do after
- Apply it via sync.Cond to coordinate listeners around a mutex-protected condition and broadcast state changes safely.
Read how 100 Go Mistakes and How to Avoid Them explains condition variable →
Modern C
A condition variable is a synchronization object on which threads wait for a predicate involving shared state to become true.
- How it is used
- Coordinates conditional processing dependencies between threads that share protected state.
- How deep it goes
- Explains mutex association, timed and untimed waits, predicate loops, early returns, signaling, initialization, and destruction.
- Examples
- Uses accounting, updating, and drawing threads whose iteration, completion, and input predicates govern calls to cnd_t waits.
- What you can do after
- Apply condition variables through cnd_t waits, mutex-protected predicates, repeated checks, and targeted signals.
Also covered in
What to know first
- lockingLocking is a synchronization technique that controls concurrent access to a shared resource or protected operation.
- threadA thread is a distinct sequence of execution with its own control flow and state, managed or scheduled by a processor, runtime, or operating system.
- critical sectionA critical section is a region of program execution that accesses shared state or resources and requires coordination to prevent conflicting concurrent operations.
- command-line argumentA command-line argument is a value or option supplied to a program when it is invoked, providing input or affecting its behavior.
Related concepts
- lockinguses · Locking is a synchronization technique that controls concurrent access to a shared resource or protected operation.
- cnd_broadcastused by · cnd_broadcast is a C threads-library function that wakes all threads waiting on a condition variable.
- cnd_signalused by · cnd_signal is a C threads-library function that wakes one thread waiting on a condition variable.
- cnd_timedwaitused by · cnd_timedwait is a C threads-library function that atomically releases a mutex while waiting on a condition variable, then reacquires it after notification or an absolute timeout.
- futurealternative to · A future is an abstraction representing an asynchronous computation’s result, which may become available later and may be pending or complete.
Questions about condition variable
What is condition variable?
A condition variable is a synchronization object that allows threads to wait for a shared-state condition and be notified when it may have changed.
Which Manning books explain condition variable?
Learn Concurrent Programming with Go, Modern C, Third Edition, C++ Concurrency in Action, Second Edition, Object-Oriented Software Design in C++, Software Design for Python Programmers, 100 Go Mistakes and How to Avoid Them and Modern C.
What should I know before learning condition variable?
Manning books that teach condition variable usually introduce locking, thread, critical section and command-line argument first.
What is the difference between condition variable and future?
A condition variable is a synchronization object that allows threads to wait for a shared-state condition and be notified when it may have changed. A future is an abstraction representing an asynchronous computation’s result, which may become available later and may be pending or complete.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.






