Skip to main content
gamedev.net
🔒 Locked

boost::thread

Started by i_is_want_smart 4 replies 2.5k views
Original Post
i_is_want_smart
i_is_want_smart
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?
ddlox
ddlox
If you're doing RAII using a boost::shared_ptr 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
CmpDev
CmpDev
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".
Antheus
Antheus
Quote:
Original post by i_is_want_smart
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.


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.

the_edd
the_edd
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.
i_is_want_smart
i_is_want_smart
Ah, ok, interruption does seem to make more sense and looks easier to manage, thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.