Multi-thread detach

Separate from the thread execution thread object, allowing execution independently continue. Once the thread exits, it releases all allocated resources. (Two threads that are independent from each other)

Call  detach later,  * the this no longer possess any thread.


#include <iostream> #include <chrono> #include <thread> void independentThread() { std::cout << "Starting concurrent thread.\n"; std::this_thread::sleep_for(std::chrono::seconds(2)); std::cout << "Exiting concurrent thread.\n"; } void threadCaller() { std::cout << "Starting thread caller.\n"; std::thread t(independentThread); t.detach(); //Separation, the main thread and the child thread independently of one another: join does not appear () kind of waiting for a child thread ends then execute the main thread; it will not appear nothing to operate, after the main thread to kill the child thread, reported abort error . // may be joinable the thread object must be destroyed before they join the main thread or set it detached, or will report abort error. std :: :: this_thread sleep_for (std :: :: seconds the Chrono ( 1 )) ; STD :: COUT << " . Thread Caller the Exiting \ n- " ; } int main () { threadCaller (); STD :: :: sleep_for, which this_thread (STD :: :: Chrono seconds The ( . 5 )); }

 

Guess you like

Origin www.cnblogs.com/Stephen-Qin/p/11373946.html