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

前端 未结 4 1748
孤街浪徒
孤街浪徒 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 10:08

    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.

提交回复
热议问题