如何检查boost线程正在运行和杀死它
How to check boost thread is running and Kill it
在我的程序中,它启动一个boost线程,并将处理程序保持为主线程的成员。当用户按下取消按钮时,我需要检查启动的线程是否仍在运行,如果它正在运行,则需要杀死该特定线程。下面是伪代码。
作弊线程
int i =1;
boost::thread m_uploadThread = boost::thread(uploadFileThread,i);
这是用来检查线程是否仍在运行的方法,但它没有工作
boost::posix_time::time_duration timeout = boost::posix_time::milliseconds(2);
if (this->uploadThread.timed_join(timeout)){
//Here it should kill the thread
}
返回值true表示线程在调用超时之前完成。看起来你想要的是
if(!this->uploadThread.timed_join(timeout))
如果要停止线程,可以使用:
my_thread.interrupt();
为了使它工作,你必须在你想要线程的函数在你中断时停止的点上设置一个中断点。
注意:它自己的中断不会停止线程,它只是设置一个标志,当到达中断点时线程被中断。如果没有找到中断点,线程不停止。
你还可以处理中断异常boost::thread_interrupted
,这样你就可以根据线程是否被中断来做事情。
例如,让我们假设下一个代码在线程函数中:
try
{
//... some important code here
boost::this_thread.interruption_poit(); // Setting interrutption point.
}
catch(boost::thread_interrupted&)
{
// Now you do what ever you want to do when
// the thread is interrupted.
}
相关文章:
- 使用基类中的派生方法运行线程,而无需使用模板
- 使用 ubuntu 终端在 c++ 上运行线程类的问题
- 将项目添加到队列时运行线程
- 运行线程和线程对象之间的关系
- 在 gtest 中运行线程
- 是否有一种可接受的运行线程或按顺序执行的方法
- 从类中运行线程
- 如何在第三方函数调用之前同时运行线程
- 在优先级最低的核心中运行线程
- 在C++中并行运行线程
- 在C 11中的背景中运行线程
- 在运行线程中处理事件
- 使用对象生存期运行线程
- WINAPI CreateThread并不总是运行线程
- 树莓派与 c++ 运行线程,在网络上发布数据
- 使用变量类型 std::function 运行线程
- Eclipse CDT 多线程调试不是最佳的 - 如何以独占方式运行线程
- 在 C 语言的后台运行线程
- 正在运行线程
- 在远程计算机上运行线程