38 lines
894 B
Rust
38 lines
894 B
Rust
|
use std::{
|
||
|
sync::{Arc, Condvar, Mutex},
|
||
|
thread,
|
||
|
time::Duration,
|
||
|
};
|
||
|
|
||
|
fn main() {
|
||
|
let pair = Arc::new((Mutex::new(false), Condvar::new()));
|
||
|
let pair_clone1 = Arc::clone(&pair);
|
||
|
let pair_clone2 = Arc::clone(&pair);
|
||
|
|
||
|
thread::spawn(move || {
|
||
|
let (lock, cvar) = &*pair_clone1;
|
||
|
let mut started = lock.lock().unwrap();
|
||
|
*started = true;
|
||
|
std::thread::sleep(Duration::from_secs(3));
|
||
|
cvar.notify_all();
|
||
|
});
|
||
|
|
||
|
thread::spawn(move || {
|
||
|
let (lock, cvar) = &*pair_clone2;
|
||
|
let mut started = lock.lock().unwrap();
|
||
|
while !*started {
|
||
|
println!("waiting 2");
|
||
|
started = cvar.wait(started).unwrap();
|
||
|
}
|
||
|
println!("Thread 2 proceeding...");
|
||
|
});
|
||
|
|
||
|
let (lock, cvar) = &*pair;
|
||
|
let mut started = lock.lock().unwrap();
|
||
|
while !*started {
|
||
|
println!("waiting 1");
|
||
|
started = cvar.wait(started).unwrap();
|
||
|
}
|
||
|
println!("Thread 1 proceeding...");
|
||
|
}
|