c++ - 多线程中的join先后顺序问题
迷茫
迷茫 2017-04-17 11:44:27
0
2
1018

join会阻塞当前线程,但是如下实例的输出:

    thread t1([](){
                this_thread::sleep_for(chrono::milliseconds(500));
                cout<<"t1"<<endl;
            });

    thread t2([](){
                cout<<"t2"<<endl;
            });

    t1.join();
    cout<<"main"<<endl;
    t2.join();

输入先后为什么是:t2 t1 main

编译工具:clang++

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
伊谢尔伦

Join blocks the thread that calls the join function. Its purpose is to wait for and confirm the completion of the execution of threads t1 and t2. It cannot guarantee the execution order of multiple threads. Similar to the waitpid function of the process. What can control the order of thread execution is thread mutex and condition

迷茫

The function of

join is to "determine that the thread being joined has ended", and it has nothing to do with the order of thread execution.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template