据我所知,消费者在等待信号时要检查条件变量。
Consumer:
lock.Lock()
while(condition) {
cond.Wait()
}
lock.Unlock()
Producer:
lock.Lock()
changeCondition()
cond.Signal() or cond.Broadcast()
lock.Unlock()
为什么不能:
Consumer:
lock.Lock()
cond.Wait()
lock.Unlock()
Producer:
lock.Lock()
changeCondition()
if(condition) {
cond.Signal() or cond.Broadcast()
}
lock.Unlock()