下面是一些关于CPPConference.com的C ++条件变量的代码示例:
std :: mutex m;std :: condition_variable cv;std :: string数据;bool ready = false;bool processed = false;
无效……
是
链接是正确的,如果线程通知都有锁,那么被通知的线程必须阻塞,直到通知线程释放锁。在多核处理器中,这是不必要的延迟。
你的比较是有缺陷的,因为它缺少细节。有两个线程同时运行,比较省略。
在解锁之前放置notify_one:
notifying thread: notify -> eventually release lock notified thread: awaken -> attempt to acquire lock and fail -> block until lock available -> acquire lock after notifying thread releases it
解锁后放置notify_one:
notifying thread: notify notified thread: awaken -> attempt to acquire lock and succeed