关于std::condition_variable需要注意的地方spurious wake-ups

来源:互联网 发布:凭都网软件 编辑:程序博客网 时间:2024/06/04 23:24

在使用条件变量的时候,一定要放在一个循环里,这是为什么呢?因为spurious wake-ups

什么是spurious wake-ups?

  • Because of some complications in making the condition wake-up completely predictable on multiprocessor systems, spurious wake-ups can occur. That means a thread is awaken even if nobody signaled the condition variable. Therefore it is necessary to check if the condition is still true after the thread has awaken. And since spurious wake-ups can occur multiple times, that check must be done in a loop.
所以,条件变量总是类似这样的形式的代码:

std::condition_variable cv;while(!g_bSignal){    cv.wait(lock);}


原创粉丝点击