Reputation: 1419
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?
Upvotes: 10
Views: 22166
Reputation: 32920
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.
Upvotes: 18
Reputation: 1605
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.
Upvotes: 22