Why .join is still necessary when all other thread have finished before the main thread?

前端 未结 4 1739
孤街浪徒
孤街浪徒 2021-02-06 09:11

Learning C++ multi-threading.
In my example, thread helper1 and helper2 have finished executing before the main thread finished. Howev

4条回答
  •  有刺的猬
    2021-02-06 09:55

    Based on the reference, underlying thread must be joined or detached at the time the destructor is called. The destructor is invoked when main exits, and probably assumes that join or detach has been called.

    The code should also not crash, as long as the following two lines are somewhere after helper1 and helper2 are constructed.

    helper1.detach()
    helper2.detach()
    

提交回复
热议问题