InterviewSolution
| 1. |
What Does Joining C++11 Threads Mean? Alternatively What Does The Std::thread::join() Do? |
|
Answer» A call to std::thread::join() blocks until the thread on which join is called, has finished executing. In each of the examples above, the join() call ENSURES that the main method WAITS for the execution of the spawned threads to FINISH before it can exit the application. On the other hand, if we do not call join() after creating a thread in the above case, the main function will not wait for the spawned thread to complete before it tears down the application. If the application tears down before the spawned thread finishes, it will TERMINATE the spawned thread as well, even if it has not finished executing. This can leave data in a very INCONSISTENT state and should be avoided at all cost. A call to std::thread::join() blocks until the thread on which join is called, has finished executing. In each of the examples above, the join() call ensures that the main method waits for the execution of the spawned threads to finish before it can exit the application. On the other hand, if we do not call join() after creating a thread in the above case, the main function will not wait for the spawned thread to complete before it tears down the application. If the application tears down before the spawned thread finishes, it will terminate the spawned thread as well, even if it has not finished executing. This can leave data in a very inconsistent state and should be avoided at all cost. |
|