c:c_threads:transfer_ownership_of_threads_at_runtime
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
c:c_threads:transfer_ownership_of_threads_at_runtime [2021/03/05 11:48] – peter | c:c_threads:transfer_ownership_of_threads_at_runtime [2021/06/09 11:07] (current) – peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== C - C++ Threads - Transfer ownership of threads at runtime ====== | ====== C - C++ Threads - Transfer ownership of threads at runtime ====== | ||
- | std::move can be called to move the ownership of the underlying thread resource from one std::thread object to another. | + | **std::move** can be called to move the ownership of the underlying thread resource from one std::thread object to another. |
---- | ---- | ||
Line 34: | Line 34: | ||
This shows that the main() thread is blocked until the t1 thread completes. | This shows that the main() thread is blocked until the t1 thread completes. | ||
+ | |||
+ | A **join** blocks the thread that called it; in this example the thread that call the t1 thread is the main() thread. | ||
+ | |||
+ | This may result in the application freezing. | ||
+ | |||
</ | </ | ||
Line 64: | Line 69: | ||
{ | { | ||
std::thread t1(Func1); | std::thread t1(Func1); | ||
+ | |||
+ | // Pass the responsibility of monitoring the t1 thread to t2. | ||
std::thread t2(Func2, std:: | std::thread t2(Func2, std:: | ||
// Do a bunch of other processing without waiting for t1 to finish. | // Do a bunch of other processing without waiting for t1 to finish. | ||
- | // Instead now the responsibility of monitoring the t1 thread is passed to t2. | ||
- | |||
std::cout << " | std::cout << " | ||
Line 81: | Line 86: | ||
**NOTE: | **NOTE: | ||
- | A join() blocks the thread that called it, in this example the main() thread. | + | Ownership |
- | + | ||
- | This may result in the application freezing. | + | |
- | + | ||
- | To overcome this issue, ownership | + | |
</ | </ |
c/c_threads/transfer_ownership_of_threads_at_runtime.1614944880.txt.gz · Last modified: 2021/03/05 11:48 by peter