【发布时间】:2020-02-25 09:36:15
【问题描述】:
上下文:
在我看到的关于使用std::condition_variable::wait() 的每个示例中,包括来自cppreference.com 的示例,从来没有任何同步机制用于保护谓词评估免受数据竞争的影响。
例如:
std::mutex m;
std::condition_variable cv;
int i = 0;
void waiting_func()
{
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, [](){return i > 0;}); // No lock/unlock around the access of the global and shared variable i.
// ...
}
问题:
如果没有这样的同步,即使来自有信誉的来源的例子,我猜那是因为没有必要。
但我想知道为什么? std::condition_variable::wait() 如何评估谓词使其是线程安全的?
我的想法:
我想出了两种可能:
- ) 要么,谓词保证被原子评估(我从未读过这样的东西,因此我的问题)
- ) 或者,当发送 notify 信号时,
std::condition_variable::wait()函数会在 评估谓词之前重新获取互斥锁。
在 2.) 点的情况下,如果修改i(并调用std::condition_variable::notify_one())的线程在这样做之前锁定互斥锁m,则可能是安全的。
例如:
void modify_func()
{
{
std::scoped_lock<std::mutex> lk(m); // Acquire the mutex
i += 1; // Modify i
} // Release the mutex
cv.notify_one();
}
当然,还有一种可能是我的理解是完全错误的,当时我没抓住重点。
无论如何,我真的很惊讶我在文档中找不到任何关于它的详细信息。
【问题讨论】:
-
也许你可以在我的问题Use of mutex after condition variable has been notified中找到一些其他信息
标签: c++ predicate condition-variable