Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using boost::thread::interrupt(), do you *need* to catch the thread_interrupted exception?

I've got several long running boost threads that I want to be able to shut down by interrupting them. All of the documentation I can find says that you can catch the thread_interrupted exception, but it doesn't really say what happens if you don't. I would assume it kills the thread (and hopefully the thread gets properly cleaned up). But then does the exception die off with the thread? Or does it get passed up to the main thread and kill it too?

like image 587
Rob W. Avatar asked Jun 16 '11 16:06

Rob W.


1 Answers

The exception is just like any other C++ exception. If you choose not to catch it, it will cause the same effect as any other unhandled exception.

If left uncaught, it will not propagate to the main thread, but could cause other undesirable behaviour. On Visual C++, by default this will terminate your process, for example.

In general, defensive programming practice would dictate that you should catch any exception your code can throw - just the same as you would check for error from an OS API that you called.

There's a backgrounder on interruption here by the person who wrote a lot of the Boost.Thread code.

like image 124
Steve Townsend Avatar answered Oct 20 '22 18:10

Steve Townsend