3、 Join, detach

Time:2021-5-8

Join:

#include<thread>
#include<iostream>
#include <windows.h>

void myPrintf() {
    Sleep(1000);
    std::cout << "threadRun" << std::endl;
    return;
}

int main()
{
    std::thread t(myPrintf);
    t.join();   //15
    std::cout << "main" << std::endl;
    system("pause");
    return 0;
}
/*Output:
threadRun
main
Please press any key to continue
*/

Although the sub thread sleeps for one second, the main thread does not continue to execute until the sub thread ends at line 15.
t. Join () means that the main thread is blocked here, and it will continue to execute until the child thread t ends,

Detach:

#include<thread>
#include<iostream>
#include <windows.h>

void myPrintf() {
    Sleep(1000);
    std::cout << "threadRun" << std::endl;
    return;
}

int main()
{
    std::thread t(myPrintf);
    t.detach();   //16
    std::cout << "main" << std::endl;
    system("pause");
    return 0;
}
/*Output:
main
Please press any key to continue
*/

When the main thread ends, the child thread outputs threadrun, which is still executing,
16 lines of T. detach () means that the main thread and the sub thread T are executed separately and do not affect each other.

be careful:

After starting a thread, you need to be clear whether you want to wait for the thread to end (join join) or let it run autonomously (detach). If the decision is made after the destruction of the STD:: thread object (note that this is after the destruction of the STD:: thread object, not after the end of the child thread), an exception will be triggered (when the STD:: thread object is destroyed, the destructor of STD:: thread will call STD:: terminate(), and then the decision will trigger the corresponding exception)

separate

If you want to separate a thread, you can directly use detach () after the thread starts, but you should ensure the validity of the accessible data before the thread ends.

#include<thread>
#include<iostream>
#include<windows.h>
void fun(int & i) {
    Sleep(1000);
    std::cout << i << std::endl;
    return;
}

void test() {
    int temp = 2;
    std::thread t(fun,std::ref(temp)); //13
    t.detach();                       //14
}

int main()
{
    test();
    system("pause");
    return 0;
}
/*
Please press any key to continue
*/

Line 13 passes a reference to temp to the thread function, but it outputs garbled code in the thread function. Due to the use of detach in line 14, the two threads are separated. When the test function is finished, the local variable temp will be destroyed, but the thread function is still running, and the destroyed variables are accessed through the reference I of temp. Therefore, try to avoid using a function that can access local variables to create a thread, unless you are very sure that the thread will end before the function is completed.

join

If you intend to wait for the corresponding thread, you need to carefully select the location of calling join (), including whether join can be correct if there is an exception (join () is invoked during the exception handling process).

Recommended Today

Rendering engine of browser (WebKit)

preface The three things behind the browserThe introduction in the last article gives you a vague understanding of the composition of the browser What is a browser? Basic architecture of chrome Browser main components What is the browser kernel? JavaScript (JS) engine rendering engine Today, let’s do the WebKit for learning and discussion. First of […]