如何杀死或终止一个boost线程
How to kill or Terminate a boost Thread
我想终止或杀死boost线程。代码在这里:
DWORD WINAPI StartFaceDetector(LPVOID temp)
{
int j=0;
char **argv1;
QApplication a(j,argv1);//add some thread here
gui::VisualControl w;
t=&w;
boost::thread u(&faceThread);
w.show();
a.exec();
// I Want to close u thread here.
return 0;
}
我想在返回函数之前关闭该boost线程。
Windows:
TerminateThread(u.native_handle(), 0);
Linux/QNX/UNIX/任何支持pthread的平台:
pthread_cancel(u.native_handle());
或
pthread_kill(u.native_handle(), 9);
注意,boost作者故意省略了这一点,因为该行为依赖于平台并且没有很好地定义。但是,您并不是唯一使用此功能的人……
使用interrupt()
。另外,您应该定义中断点。线程调用interrupt()
后,一旦到达中断点之一,线程将被中断。
u.interrupt();
更多信息:
调用
interrupt()
只是在线程管理结构中为该线程设置一个标志并返回:它不等待线程实际被中断。这很重要,因为线程只能在预定义的中断点之一中断,并且线程可能从未执行中断点,因此从未看到请求。目前,中断点为:
boost::thread::join()
boost::thread::timed_join()
boost::condition_variable::wait()
boost::condition_variable::timed_wait()
boost::condition_variable_any::wait()
boost::condition_variable_any::timed_wait()
boost::this_thread::sleep()
boost::this_thread::interruption_point()
相关文章:
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 我正在将一个 std::string 传递给一个 boost 函数,该函数对该类型进行常量引用,但该值发生了变化
- 如何将 boost::mpl::vector 转换为另一个 boost::mpl::vector
- 如何编写一个 boost::spirit::qi 解析器来解析从 0 到 std::numeric_limits:<int>:max() 的整数范围?
- 创建一个 boost::线程 带有 boost::bind() 或不带
- C :如何拥有一个boost :: multi_array数组
- 为什么我必须有一个 boost::mutex for boost::condition_variable
- 在linux下使用boost::thread创建一个boost::asio工作线程
- 有没有一种方法可以为类的每个对象提供一个boost线程
- 填充一个boost元组以替代此方法
- 访问可调用对象 一个 boost::thread 是用来构造的
- 如何启动一个 boost::线程运行一个成员函数
- 如何创建一个Boost.Range来隐藏多层向量并将其公开为单个Range
- 如何将两个独立的boost::bind()组合为一个boost::函数
- 分区一个boost::range::转换后的范围适配器
- 我可以复制一个Boost::Log接收器并更改它吗?
- 将数据从调用方线程传递到另一个 boost::thread 中的方法
- 我需要删除我创建的一个boost线程吗?
- 如何编写一个 boost::spirit::qi 解析器来做'?'在正则表达式中做的事情?
- 多个async_wait从一个boost Asio deadline_timer