use std::sync::{Arc, Mutex, Condvar};
use std::thread;
use std::time::Duration;
fn main() {
println!("Hello, world!");
let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = pair.clone();
thread::spawn(move || {
thread::sleep(Duration::from_secs(1));
let &(ref lock, ref cvar) = &*pair2;
let mut started = lock.lock().unwrap();
*started = true;
cvar.notify_one();
println!("child thread {}", *started);
});
let &(ref lock, ref cvar) = &*pair;
let mut started = lock.lock().unwrap();
println!("before wait {}", *started);
while !*started {
started = cvar.wait(started).unwrap();
}
println!("after wait {}", *started);
}
1
共 3 条评论, 1 页
评论区
写评论我改成這樣,應該對你會有用...
为什么要用Condvar 为什么要和锁一起用 [/黑脸]
对以下内容的回复:
讲啥?条件变量不都是这样用的