Condvar and barriers
Quick answer
A condition variable lets a thread wait for a predicate protected by a mutex. A barrier makes a group of threads wait until all participants reach a synchronization point.
Coordinate phases and predicates
A Condvar wait releases its mutex while blocked and reacquires it before returning. Recheck the predicate because wakeups can be spurious and other threads can change state. wait_while handles the predicate loop. Update the protected condition before notification. A Barrier needs the agreed number of arrivals for each round.
use std::sync::{Mutex, Condvar};fn main() { let ready = Mutex::new(false); let cv = Condvar::new(); std::thread::scope(|s| { s.spawn(|| { *ready.lock().unwrap() = true; cv.notify_one(); }); let guard = cv.wait_while(ready.lock().unwrap(), |r| !*r).unwrap(); println!("ready: {}", *guard); });}Expected stdout (not executed):
ready: trueThe predicate works whether notification occurs before or during the wait.
use std::sync::{Barrier, atomic::{AtomicUsize, Ordering}};fn main() { let gate = Barrier::new(2); let count = AtomicUsize::new(0); std::thread::scope(|s| { s.spawn(|| { count.fetch_add(1, Ordering::SeqCst); gate.wait(); }); count.fetch_add(1, Ordering::SeqCst); gate.wait(); println!("{}", count.load(Ordering::SeqCst)); });}Expected stdout (not executed):
2Both increments precede passage through the barrier; atomic operations are explained in the next chapter.
Common mistake
Never treat one notification as proof that the predicate is true.
Try it yourself
Change the barrier to three participants and add a third arriving worker.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Condvar and barriers — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A condition variable wakes a worker but its queue remains empty. What should the worker do?
Condvar and barriers — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::sync::{Mutex, Condvar}; fn main() { let ready = Mutex::new(false); let cv = Condvar::new(); std::thread::scope(|s| { s.spawn(|| { *ready.lock().unwrap() = true; cv.notify_one(); }); let guard = cv.wait_while(ready.lock().unwrap(), |r| !*r).unwrap(); println!("ready: {}", *guard); }); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Condvar and barriers — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::sync::{Barrier, atomic::{AtomicUsize, Ordering}}; fn main() { let gate = Barrier::new(2); let count = AtomicUsize::new(0); std::thread::scope(|s| { s.spawn(|| { count.fetch_add(1, Ordering::SeqCst); gate.wait(); }); count.fetch_add(1, Ordering::SeqCst); gate.wait(); println!("{}", count.load(Ordering::SeqCst)); }); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.