Why is destructor of boost::thread detaching joinable thread instead of calling terminate() as standard suggests?

。_饼干妹妹 提交于 2019-11-29 13:40:56

问题


According to the draft C++0x standard, this code:

void simplethread()
{
    boost::thread t(someLongRunningFunction);
    // Commented out detach - terminate() expected.
    // t.detach();  
}

... should result in an terminate() call, but in current (boost 1.46.1) implementation of boost threads it doesn't, thread simply gets detached in destructor and continues on.

My question is: why?

I thought boost::thread is as much inline with draft standard as it gets.

Is there a design reason for this? Will it be changed in future versions of boost::thread?


回答1:


The reason is largely historical. boost::thread came first. The proposals for std::thread were derived from boost::thread and originally had the behavior that boost::thread does now.

However during the standardization process a significant number of people wanted std::thread::~thread() to join() in the destructor if not already joined, instead of detach(). The arguments were made for each side and the votes were taken. 50/50. More arguments were made and more votes were taken. Some people were swayed to the other position. But still 50/50.

Someone (I don't recall who) suggested terminate(). Votes were taken and though it wasn't unanimous in favor (I couldn't vote for it), it did receive enough of a majority to be called consensus.

I imagine boost::thread never changed because it had an installed user base and no one wants to unnecessarily break code for that user base.

Edit:

Ah, Rob points us to the original of this duplicate question and that answer points to N2802 which includes rationale.

I should also note that the original proposal for std::thread had thread cancellation, and ~thread() would cancel the unjoined-thread and then detach it, which made a lot of sense. This code path would normally only be chosen when the parent thread was unwinding due to an exception.



来源:https://stackoverflow.com/questions/5874493/why-is-destructor-of-boostthread-detaching-joinable-thread-instead-of-calling

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