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

荒凉一梦 提交于 2019-12-04 19:09:49

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6375121/when-using-boostthreadinterrupt-do-you-need-to-catch-the-thread-interru

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!