Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 748
Mustansar Fiaz Avatar asked May 09 '14 07:05

Mustansar Fiaz


People also ask

How do I interrupt a running thread in boost?

A running thread can be interrupted by calling the interrupt () member function on the corresponding boost::thread object. If the thread doesn't have a boost::thread object (e.g the initial thread of the application), then it cannot be interrupted.

How many times does boost kill a thread after N seconds?

3 kill boost thread after n seconds 46 Using boost thread and a non-static class function 2 Have main thread wait for a boost thread complete a task (but not finish) 592 How to install Boost on Ubuntu

What is boost thread in Java?

The boost::thread class is responsible for launching and managing threads. Each boost::thread object represents a single thread of execution, or Not-a-Thread, and at most one boost::thread object represents a given thread of execution: objects of type boost::thread are not copyable.

How do I force a thread to stop running?

This action can be done by using the interrupt () method. Whenever Thread.interrupt () is called, it sets a flag known as the interrupt status to true. This means that the thread has to stop performing further execution. The default value of this flag is false.


2 Answers

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...

like image 174
Dave McMordie Avatar answered Oct 19 '22 23:10

Dave McMordie


Use interrupt(). Also, you should define interruption points. Thread will be interrupted after calling interrupt() as soon as it reaches one of interruption points.

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. 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()
like image 36
herohuyongtao Avatar answered Oct 20 '22 00:10

herohuyongtao