How to kill or Terminate a boost Thread

Mustansar Fiaz picture Mustansar Fiaz · May 9, 2014 · Viewed 21.8k times · Source

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.

Answer

Dave McMordie picture Dave McMordie · Aug 20, 2014

On Windows:

TerminateThread(u.native_handle(), 0);

On Linux / QNX / UNIX / any platform with pthread support:

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. However, you're not the only one to ever reach for this functionality...