I create a thread in a function,and in another function,I wanna stop this thread. I have tried like this:
class Server
{
private:
boost::thread* mPtrThread;
...
public:
void createNewThread()
{
boost::thread t(...);
mPtrThread = &t;
}
void stopThread()
{
mPtrThread->interrupt();
}
}
But it's not work.How could I stop the thread?
If you want to use interrupt() you should define interruption points . Thread will be interrupted after calling interrupt() as soon as it reaches one of interruption points.
First of all, in createNewThread()
you declare a boost::thread t
in a local scope and assign its pointer to the class member mPtrThread
. After createNewThread()
finishes, t
is destroyed and mPtrThread would hold an illegal pointer.
I'd rather use something like mPtrThread = new boost::thread(...)
;
You might also want to read this article to learn more about multithreading in Boost.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.