Ownership management
From the examples discussed so far in this chapter, you might have noticed that the function that launches the thread has to wait for the thread to complete its execution using the join() function, otherwise it will call detach() with a cost of the program losing control over the thread. In modern C++, many standard types are movable, but cannot be copied; std::thread is one of them. This means that the ownership of a thread's execution can be moved between std::thread instances with the help of move semantics.
There are many situations where we want to move the ownership to another thread, for example, if we want the thread to run in the background without waiting for it on the function that created the thread. This ...
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