C++11 Multi-Threading 01

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.