std-method

Condvar::notify_all: wake all waiters

Notify every currently waiting thread when a shared condition changes. Notifications are not stored for future waiters; a persistent protected predicate makes late arrivals safe. Expected stdout (not executed): 2 Common mistake: Expecting notifications to act as stored permits.

Formal Syntax & Structure

rust syntaxrust
cv.notify_all()

Working Example

Example Snippetrust
fn main() {    use std::sync::{Condvar, Mutex};    let state = Mutex::new(false);    let cv = Condvar::new();    let count = std::thread::scope(|s| {        let a = s.spawn(|| { drop(cv.wait_while(state.lock().unwrap(), |x| !*x).unwrap()); 1 });        let b = s.spawn(|| { drop(cv.wait_while(state.lock().unwrap(), |x| !*x).unwrap()); 1 });        *state.lock().unwrap() = true; cv.notify_all();        a.join().unwrap() + b.join().unwrap()    });    println!("{count}");}
Rust Reference