且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++ 0x与Boost中的线程析构函数

更新时间:2021-12-02 09:23:44

确实是这样,并且在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.