#include <iostream><thread>void Thread_fun () { std::cout<<" I am the thread function "<<Std::endl;} int Main () { std::thread T (thread_fun); // run the thread function, the main thread is blocked here until Thread_fun () execution is complete return 0 ;}
#include <iostream><thread>void Thread_fun () { std::cout<<" I am the thread function "<<Std::endl;} int Main () { std::thread T (thread_fun); // run thread functions without blocking the main thread return 0 ;}
The console does not display any characters because the main thread is not blocked by using detach to turn on the threads, and the primary thread has finished executing.
#include <iostream><thread>void Thread_fun () { std::cout<<" I am the thread function "<<Std::endl;} int Main () { std::thread T (thread_fun); // run thread functions without blocking the main thread ///detach, you can no longer use join return 0 ;}
Conclusion: After detach, you can no longer use join
#include <iostream>#include<thread>voidThread_fun () {std::cout<<"I am a thread function"<<Std::endl;}intMain () {Std::thread T (thread_fun); T.detach (); //run thread functions without blocking the main thread if(T.joinable ()) {t.join ();//after detach, you can no longer use join } Else{std::cout<<"after detach, you can no longer use join"<<Std::endl; } return 0;}
Conclusion: You can use Joinable () to determine whether a join ()
C++11 Multi-Threading 01