当使用boost ::螺纹::中断(),你需要* *捕捉thread_interrupted例外呢? [英] When using boost::thread::interrupt(), do you *need* to catch the thread_interrupted exception?

查看:483
本文介绍了当使用boost ::螺纹::中断(),你需要* *捕捉thread_interrupted例外呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了我希望能够通过中断他们关闭几个长期运行的线程提振。所有文件我能找到的说,你的可以的赶 thread_interrupted 例外,但它并没有真正说,如果你不这样做会发生什么。
我会假设它杀死线程(希望线程才能正确清理)。但是后来没有例外死光与线程?或者它获取传递到主线程,太杀了它?

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?

推荐答案

唯一的例外是,就像任何其他C ++异常。如果您选择不抓住它,它会导致任何其他未处理的异常具有相同的效果。

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.

如果离开未捕获,也不会传播到主线程,但可能会导致其他不良行为。基于Visual C ++中,默认情况下,这将终止进程,例如。

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.

在一般情况下,防御性的编程实践将决定你应该捕获任何异常的code可以抛出 - 一样的,你会从你称为OS API检查错误

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.

有一个关于中断一份背景<一个href=\"http://www.justsoftwaresolutions.co.uk/threading/thread-interruption-in-boost-thread-library.html\">here由谁写了不少Boost.Thread code的人。

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

这篇关于当使用boost ::螺纹::中断(),你需要* *捕捉thread_interrupted例外呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆