Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions 33_concurrency/examples/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ fn main() {

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

// 通过离开作用域来释放锁,否则主线程会一直阻塞在获取锁上
{
let mut started = lock.lock().unwrap();
*started = true;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处也可以使用 drop(started);


eprintln!("I'm a happy worker!");
// 通知主线程
cvar.notify_one();
Expand All @@ -27,4 +32,7 @@ fn main() {
started = cvar.wait(started).unwrap();
}
eprintln!("Worker started!");

// 等待 worker 线程
thread::sleep(Duration::from_secs(3600));
}