What is the reason for a joinable std::thread not join automatically?

后端 未结 1 1741
别那么骄傲
别那么骄傲 2020-12-17 23:25

Sometimes it would be useful if a joinable std::thread had the hability to execute thread::join() on its destructor. See the examples below.

<
相关标签:
1条回答
  • 2020-12-18 00:09

    The reason is simply so that you are forced to think about it. If a std::thread object is destroyed due to an exception escaping the scope then a join may cause a blocking wait during stack unwinding, which is often undesirable, and can lead to deadlock if the thread that is being waited for is in turn waiting for some action on the part of the thread doing the waiting.

    By having the application terminate in this situation you as a programmer are forced to actively think about the conditions that would cause the object to be destroyed, and ensure that the thread is joined correctly.

    0 讨论(0)
提交回复
热议问题