C ++ 0x与Boost中的线程析构函数 [英] thread destructors in C++0x vs boost

查看:77
本文介绍了C ++ 0x与Boost中的线程析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几天,我正在阅读pdf 设计MT程序。它解释说,用户必须在C ++中显式调用 std :: thread 类的对象上的 detach()该对象超出范围之前为0x。如果您不调用它,将调用 std :: terminate()并且该应用程序将终止。

These days I am reading the pdf Designing MT programs . It explains that the user MUST explicitly call detach() on an object of class std::thread in C++0x before that object gets out of scope. If you don't call it std::terminate() will be called and the application will die.

我通常在C ++中使用 boost :: thread 进行线程化。如果我错了,请纠正我,但是 boost :: thread 对象在超出范围时会自动分离。

I usually use boost::thread for threading in C++. Correct me if I am wrong but a boost::thread object detaches automatically when it get out of scope.

在我看来,boost方法遵循RAII原则,而std没有。

Is seems to me that the boost approach follow a RAII principle and the std doesn't.

您知道是否有某些特殊原因吗?

Do you know if there is some particular reason for this?

推荐答案

确实是这样,并且在N3225中有关 std :: thread 析构函数的注释中对此选择进行了解释:

This is indeed true, and this choice is explained in N3225 on a note regarding std::thread destructor :

如果 joinable(),则 terminate() ,否则无效。 [注意:
在其
析构函数中隐式分离或加入
a joinable()线程可能会导致
难以调试正确性(用于分离)或
性能(用于联接)bugs
仅在引发异常
时遇到。 因此,程序员必须
来确保在线程仍可连接
时,绝不执行析构函数
-尾注]

If joinable() then terminate(), otherwise no effects. [ Note: Either implicitly detaching or joining a joinable() thread in its destructor could result in difficult to debug correctness (for detach) or performance (for join) bugs encountered only when an exception is raised. Thus the programmer must ensure that the destructor is never executed while the thread is still joinable. —end note ]

显然,委员会赞成减少两种弊端。

Apparently the committee went for the lesser of two evils.

编辑我刚刚发现这篇有趣的论文解释了为什么最初的措词是

EDIT I just found this interesting paper which explains why the initial wording :


如果 joinable(),则 detach() ,否则没有影响。

If joinable() then detach(), otherwise no effects.

已更改为先前引用的内容。

was changed for the previously quoted one.

这篇关于C ++ 0x与Boost中的线程析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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