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

前端 未结 4 619
忘了有多久
忘了有多久 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.

提交回复
热议问题