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

前端 未结 4 614
忘了有多久
忘了有多久 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:46

    The CPU can schedule the three threads ( main / thread1 / thread2 ) in any order. It might happen that your main doesn't get a time to run and your threads exit. So, you need to keep keep join in main to take care of this case. Scheduling of threads is unpredictable, unless you are using an RTOS.

    0 讨论(0)
  • 2021-02-06 09:54

    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()
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-06 10:04

    Because std::~thread calls terminate if the associated thread is still joinable:

    30.3.1.3 thread destructor [thread.thread.destr]

    ~thread();
    

    If joinable(), calls std::terminate(). Otherwise, has no effects. [ Note: Either implicitly detaching or joining a joinable() thread in its destructor could result in difficult to debug correctness (for detach) or performance (for join) bugs encountered only when an exception is raised. Thus the programmer must ensure that the destructor is never executed while the thread is still joinable. —end note]

    You need to call either .detach() or .join(). Other than that, since you cannot be sure how the operating system schedules your threads, you could end up interrupting your threads any way, so better use .join() from the beginning.

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