User Tools

Site Tools


c:c_threads:transfer_ownership_of_threads_at_runtime

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
c:c_threads:transfer_ownership_of_threads_at_runtime [2021/03/05 11:35] peterc: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 ======
  
-Thestd::thread object owns the current thread of execution.+**std::move** can be called to move the ownership of the underlying thread resource from one std::thread object to another. 
 + 
 +---- 
 + 
 +===== Problem ===== 
 + 
 +<code cpp> 
 +#include <iostream> 
 +#include <string> 
 +#include <thread> 
 +#include <functional> 
 + 
 +void Func1() 
 +
 +  std::this_thread::sleep_for(std::chrono::milliseconds(5000)); 
 +  std::cout << "Finished Func1"<< std::endl; 
 +
 + 
 +int main() 
 +
 +  std::thread t1(Func1); 
 +  t1.join(); 
 + 
 +  std::cout << "Continue Main" << std::endl; 
 + 
 +  return 0; 
 +
 +</code> 
 + 
 +<WRAP info> 
 +**NOTE:**  The **Continue Main** message is only displayed once the function Func1 finishes. 
 + 
 +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. 
 + 
 +</WRAP>
  
-std::move can be called to move the ownership of the underlying resource from one std::thread object to another. 
  
 ---- ----
  
-===== Example =====+===== Solution =====
  
 To not have to wait for a thread to finish, pass the thread to another function which will wait for the thread to finish and execute some action once the execution is done. To not have to wait for a thread to finish, pass the thread to another function which will wait for the thread to finish and execute some action once the execution is done.
Line 32: 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::move(t1));   std::thread t2(Func2, std::move(t1));
  
   // 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 << "Continue Main" << std::endl;
  
   // Finally wait for t2 to finish.   // Finally wait for t2 to finish.
Line 45: Line 84:
  
 <WRAP important> <WRAP important>
-**NOTE:**  A join() blocks the thread that called it, in this example the main() thread+**NOTE:**  Notice that now the **Continue Main** message is displayed even before the thread function finishes.
- +
-This may result in the application freezing.+
  
-To overcome this issue, ownership of the thread is passed to a different thread, in this example, t2, allowing the main() thread to not be blocked.+Ownership of the t1 thread is passed to a different thread, in this example, t2, allowing the main() thread to not be blocked.
  
 </WRAP> </WRAP>
c/c_threads/transfer_ownership_of_threads_at_runtime.1614944120.txt.gz · Last modified: 2021/03/05 11:35 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki