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

前端 未结 4 609
忘了有多久
忘了有多久 2021-02-06 09:13

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:57

    I'd say that your question doesn't make sense, because it's based on a false assumption. The only way to know that a thread has finished is when the thread's join() returns. Before join() returns, it is not the case that "the thread has finished". It may be true that some statement within the thread's execution has completed (e.g. the printing of a message, or better, the writing of an atomic variable), but the completion of the thread function itself is not measurable in any way other than by joining.

    So none of the threads "have finished" until you join them.

提交回复
热议问题