简体   繁体   English

如何杀死或终止增强线程

[英]How to kill or Terminate a boost Thread

I want to terminate or kill boost thread. 我想终止或终止加速线程。 code is here: 代码在这里:

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;   
}

I want to close that boost thread before return of function. 我想在函数返回之前关闭该升压线程。 Thanks in Advance. 提前致谢。

On Windows: 在Windows上:

TerminateThread(u.native_handle(), 0);

On Linux / QNX / UNIX / any platform with pthread support: 在Linux / QNX / UNIX /具有pthread支持的任何平台上:

pthread_cancel(u.native_handle());

or 要么

pthread_kill(u.native_handle(), 9);

Note that boost authors intentionally left this out as the behaviour is platform-dependent and not well defined. 请注意,由于行为是依赖于平台且定义不明确的,因此boost的作者有意忽略了这一点。 However, you're not the only one to ever reach for this functionality... 但是,您并不是唯一可以使用此功能的人...

Use interrupt() . 使用interrupt() Also, you should define interruption points . 另外,您应该定义中断点 Thread will be interrupted after calling interrupt() as soon as it reaches one of interruption points. 一旦到达中断点之一,线程将在调用interrupt()之后被中断。

u.interrupt();

More info : 更多信息

Calling interrupt() just sets a flag in the thread management structure for that thread and returns: it doesn't wait for the thread to actually be interrupted. 调用interrupt()只是在该线程的线程管理结构中设置一个标志并返回:它不等待该线程实际被中断。 This is important, because a thread can only be interrupted at one of the predefined interruption points, and it might be that a thread never executes an interruption point, so never sees the request. 这很重要,因为线程只能在预定义的中断点之一处被中断,并且可能是线程从不执行中断点,因此从不看到请求。 Currently, the interruption points are: 当前,中断点为:

  • 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM