Thread join
In the Hello World example, you might have noticed the use of t.join() at the end of main() before leaving from the function. The call to join() on the associated thread instance ensures that the launched function will wait until the background thread completes its execution. In the absence of join, the thread will be terminated before the thread starts until the current context is finished (their child threads will also be terminated).
join() is a direct function, either waiting for the thread to finish or not. To get more control over the thread, we have other mechanisms such as mutex, condition variables, and futures, and they will be discussed in the later sections of this chapter and the next chapter. The call to join()
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access