Closed Heap-Hop closed 12 months ago
Hello, 抱歉上次提交时没有详细说明。
原来例子中,在新线程的代码块中会出现多次获取和释放锁,这个应该是没必要操作。且可能无法让读者真正理解wait方法的作用。
参考官方文档中Condvar的用例 https://doc.rust-lang.org/stable/std/sync/struct.Condvar.html
// Wait for the thread to start up.
let (lock, cvar) = &*pair;
let mut started = lock.lock().unwrap();
while !*started {
started = cvar.wait(started).unwrap();
}
以及对于wait方法的说明 https://doc.rust-lang.org/stable/std/sync/struct.Condvar.html#method.wait
This function will atomically unlock the mutex specified (represented by guard) and block the current thread. This means that any calls to notify_one or notify_all which happen logically after the mutex is unlocked are candidates to wake this thread up. When this function call returns, the lock specified will have been re-acquired.
调用wait()时传入guard进去会自动暂时“释放”锁,直到notify后会重新返回guard。
所以我参照官方用例尝试进行了一些修改调整。
兄弟能否给一下修改的上下文和理由 :D