gpt4 book ai didi

c++ - boost::thread 异步中断

转载 作者:行者123 更新时间:2023-11-30 03:50:43 25 4
gpt4 key购买 nike

我想在开关中使用线程。这是可能的还是有另一种方法可以在程序的其他地方中断线程?

switch (event_element)    // dispatch event handler
{
case 1: // main task thread
{
boost::thread thr_check_db_task (check_db_task);
break;
}
case 2:
{
std::cerr << "DATABASE CONNECTION ERROR" << std::endl;
thr_check_db_task.interrupt (); // **COMPILE ERROR**
mysql_connection_error ();
break;
}
default:
break;
}

谢谢。

最佳答案

你需要做的是将这个线程对象作为所属类的成员变量,以便在该类的函数调用之间共享。

例如,

#include <boost/thread/thread.hpp>

class tMyApp
{
struct callable
{
void operator()()
{
// Do something and wait to be interrupted
while (true)
{
/* .. */
}
}
};

public:
tMyApp(): m_thread(m_threadProc)
{
/* do something.. */

m_thread.interrupt();
}

private:
callable m_threadProc;
// The thread object
boost::thread m_thread;
};

关于c++ - boost::thread 异步中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31600018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com