This topic is 3568 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I've just been toying around with the boost/thread library, and I encountered a problem. After a thread is created, there seems to be no way of terminating it except by returning from within the thread function. I've written threads with win32 before and I know there is a TerminateThread function. Is there a boost::thread equivalent?

Share on other sites
If you're doing RAII using a boost::shared_ptr<boost::thread> then you don't need a terminator, exiting is just fine. Reference count on the thread will be adjusted internally for you and destroyed if needed ;-)

And if you're not doing RAII, then you should revise and do :-D

Cheers

Share on other sites
In what situation have you called TerminateThread in the past, you have to know the ins and outs of what the thread is doing and the resources it can control to call this function. In the words of Mr Mackey "TerminateThread is bad".

Share on other sites
Quote:
 Original post by i_is_want_smartI've just been toying around with the boost/thread library, and I encountered a problem. After a thread is created, there seems to be no way of terminating it except by returning from within the thread function.

Yes, that's the appropriate method of exiting a thread.

volatile bool running;void thread_func() {  while (running) {    // foo  }};....running = true;boost::thread t(&thread_func());...running = false;t.join();

Terminate thread usually indicates inadequate flow control. There's effectively never a situation where one would want to force a thread to terminate.

Share on other sites
The boost thread library since 1.35 has a feature to interrupt threads. This is a much better alternative.

A boost::thread_interrupted exception will be thrown at the next interruption point inside your thread. This gives your destructors an opportunity to clean up, which naive thread termination does not.

Share on other sites
Ah, ok, interruption does seem to make more sense and looks easier to manage, thanks.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 12
• 9
• 12
• 37
• 12
• Forum Statistics

• Total Topics
631419
• Total Posts
2999984
×

Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!