How to Use Barrier and Condvar in Rust

Use Mutex to protect shared state and Condvar to signal threads when that state changes.

Use std::sync::Mutex to protect shared data and std::sync::Condvar to wait for a condition to change.

use std::sync::{Arc, Mutex, Condvar};
use std::thread;

let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = pair.clone();

thread::spawn(move || {
    let (lock, cvar) = &*pair2;
    let mut started = lock.lock().unwrap();
    *started = true;
    cvar.notify_one();
});

let (lock, cvar) = &*pair;
let mut started = lock.lock().unwrap();
while !*started {
    started = cvar.wait(started).unwrap();
}