From the course: Parallel and Concurrent Programming with C++ Part 1

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Detached thread: C++ demo

Detached thread: C++ demo - C++ Tutorial

From the course: Parallel and Concurrent Programming with C++ Part 1

Start my 1-month free trial

Detached thread: C++ demo

- [Narrator] In this program, to demonstrate detaching a thread, we defined a function called kitchen on line seven which represents a period background task like garbage collection. The kitchen cleaner uses an infinite while loop to continuously print a message that Olivia cleaned the kitchen once every second. Down in the program's main function, I start a new kitchen cleaner thread named Olivia on line 15. Then the thread prints a series of three messages that Barron is cooking, followed by a message that Barron is done on line 20, before finally waiting for Olivia's thread to finish and join. If I run the program, I see those messages from Barron and Olivia displayed. But after the main thread reaches the end and prints its final Barron is done message, the program doesn't exit because the kitchen cleaner thread is still going strong and it will continue to run forever. So I'll terminate it by pressing Control +…

Contents