根据文档,是否调用了安全的Wait()方法sync.Cond,它Unlock()首先执行?
假设我们正在检查要满足的条件:
func sample() {
cond = &sync.Cond{L: &sync.Mutex{}} // accessible by other parts of program
go func() {
cond.L.Lock()
for !condition() {
cond.Wait()
}
// do stuff ...
cond.L.Unlock()
}()
go func() {
cond.L.Lock()
mutation()
cond.L.Unlock()
cond.Signal()
}()
}
和:
func condition() bool {
// assuming someSharedState is a more complex state than just a bool
return someSharedState
}
func mutation() {
// assuming someSharedState is a more complex state than just a bool
// (A) state mutation on someSharedState
}
既然Wait()执行了Unlock,那么 (A) 应该有自己的锁定吗?还是原子性?
神不在的星期二
相关分类