How to use condvar for thread synchronization

Use Condvar with a Mutex to block threads until a specific condition is signaled by another thread.

Use Condvar with a Mutex to block a thread until a specific condition is met, then wake it up with notify_one or notify_all.

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

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

// Thread 1: Wait for condition
let (lock, cvar) = &*pair;
cvar.wait(lock.lock().unwrap()).unwrap();

// Thread 2: Signal condition
let (lock, cvar) = &*pair2;
*lock.lock().unwrap() = true;
cvar.notify_one();