C++ 如何使用 boost 条件变量等待线程完成处理?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16907072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:45:21  来源:igfitidea点击:

How do I use a boost condition variable to wait for a thread to complete processing?

c++multithreadingboost

提问by MM.

I am using a conditional variable to stop a thread until another thread has completed processing it's task queue (long story). So, on one thread I lock and wait:

我正在使用条件变量来停止一个线程,直到另一个线程完成处理它的任务队列(长话短说)。因此,在一个线程上我锁定并等待:

boost::mutex::scoped_lock lock(m_mutex);
m_condition.wait(lock);

Once the other thread has completed it's tasks, it signals the waiting thread as follows:

一旦另一个线程完成了它的任务,它就会向等待线程发出如下信号:

boost::mutex::scoped_lock lock(m_parent.m_mutex);
m_parent.m_condition.notify_one();

The problem I am seeing is that the waiting thread does not stop waiting unless I set a breakpoint on the instructions following it (I am using xcode, fyi). Yes, this seems strange. Does anyone know why this might be happening? Am I mis-using the condition variable?

我看到的问题是等待线程不会停止等待,除非我在它后面的指令上设置断点(我使用的是 xcode,仅供参考)。是的,这看起来很奇怪。有谁知道为什么会发生这种情况?我是否误用了条件变量?

回答by Wandering Logic

Yes, you are misusing the condition variable. "Condition variables" are really just the signaling mechanism. You also need to be testing a condition. In your case what might be happening is that the thread that is calling notify_one()actually completes before the thread that calls wait()even starts. (Or at least, the notify_one()call is happening before the wait()call.) This is called a "missed wakeup."

是的,您滥用了条件变量。“条件变量”实际上只是信号机制。您还需要测试条件。在您的情况下,可能发生的情况是调用notify_one()的线程实际上在调用的线程wait()开始之前完成。(或者至少,notify_one()呼叫发生在wait()呼叫之前。)这称为“错过唤醒”。

The solution is to actually have a variable which contains the condition you care about:

解决方案是实际上有一个包含您关心的条件的变量:

bool worker_is_done=false;

boost::mutex::scoped_lock lock(m_mutex);
while (!worker_is_done) m_condition.wait(lock);

and

boost::mutex::scoped_lock lock(m_mutex);
worker_is_done = true;
m_condition.notify_one();

If worker_is_done==truebefore the other thread even starts waiting then you'll just fall right through the while loop without ever calling wait().

如果worker_is_done==true在另一个线程开始等待之前,那么您将直接通过 while 循环而无需调用wait().

This pattern is so common that I'd almost go so far as to say that if you don't have a whileloop wrapping your condition_variable.wait()then you always have a bug. In fact, when C++11 adopted something similar to the boost::condtion_variable they added a new kind of wait() that takes a predicate lambda expression (essentially it does the whileloop for you):

这种模式非常普遍,以至于我几乎要说,如果你没有一个while循环来包装你的,condition_variable.wait()那么你总是有一个错误。事实上,当 C++11 采用类似于 boost::condtion_variable 的东西时,他们添加了一种新的 wait() ,它采用谓词 lambda 表达式(本质上它while为您执行循环):

std::condition_variable cv;
std::mutex m;
bool worker_is_done=false;


std::unique_lock<std::mutex> lk(m);
cv.wait(lk, []{return worker_is_done;});

回答by Fernando

I had implemented an example that illustrates how to use boost condition, based in the discussion.

基于讨论,我已经实现了一个示例来说明如何使用提升条件。

#include <iostream>

#include <boost/asio.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>

boost::mutex io_mutex;
bool worker_is_done = false;
boost::condition_variable condition;

void workFunction()
{
    std::cout << "Waiting a little..." << std::endl;
    boost::this_thread::sleep(boost::posix_time::seconds(1));
    worker_is_done = true;
    std::cout << "Notifying condition..." << std::endl;
    condition.notify_one();
    std::cout << "Waiting a little more..." << std::endl;
    boost::this_thread::sleep(boost::posix_time::seconds(1));
}

int main()
{
    boost::mutex::scoped_lock lock(io_mutex);
    boost::thread workThread(&workFunction);

    while (!worker_is_done) condition.wait(lock);
    std::cout << "Condition notified." << std::endl;
    workThread.join();
    std::cout << "Thread finished." << std::endl;

    return 0;
}

Boost condition variable example

Boost条件变量示例