Advanced · Module 11
Threads, Mutexes and Condition Variables
Run work on several threads, protect shared data with mutexes, and coordinate producers and consumers safely.
One thread answers one user; a server answers thousands. This module turns thread callables, reader-writer locks, and producer-consumer queues into working discipline, then uses the queue as the heart of the thread-pool project.
- 1Launching and Joining ThreadsPro — members onlyLaunch threads from five callable forms and join every thread, so ownership, argument passing and lifetime stay correct from the first program.9 min
- 2Mutual Exclusion: mutex, lock_guard and unique_lockPro — members onlyProtect shared counters and caches with mutex ownership, compare lock_guard with unique_lock, and scale reads with shared_mutex.10 min
- 3Deadlock: What It Is and How std::lock Avoids ItPro — members onlyRecognize the four deadlock conditions, reproduce opposite-order locking, and fix it with std lock and scoped_lock.9 min
- 4Condition Variables: Waiting for WorkPro — members onlyPark consumer threads until a producer enqueues work, using a mutex, a condition variable and a predicate loop that survives spurious wakeups.12 min
- 5Project: A Producer-Consumer Task QueuePro — members onlyAssemble a fixed thread pool around a guarded task queue, where producers submit jobs and consumers drain them until done and join.12 min
- 6Modern Async Coordination: jthread, stop_token and barriersPro — members onlyMake worker lifetime, cancellation and phase completion explicit with C++20 thread-coordination primitives.12 min