MCPcopy Create free account
hub / github.com/EricPengShuai/Interview / main

Function main

STL/thread.cpp:63–111  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

61}
62
63int main()
64{
65 thread th1(t1); // 实例化一个线程对象th1,使用函数t1构造,然后该线程就开始执行了(t1())
66 thread th2(t2);
67
68 th1.join(); // 必须将线程join或者detach 等待子线程结束主进程才可以退出
69 th2.join();
70 /* join 输出结果: (不是固定的,使用 mutex 也不一定固定,因为 sleep)
71 * t1 111
72 * t2 222
73 * t1 111
74 * t2 222
75 * t1 111
76 * t2 222
77 * t2 222
78 * t2 222
79 * t2 222
80 * here is main
81 * */
82
83// th1.detach();
84// th2.detach();
85 /* detach 输出结果:
86 * here is main
87 *
88 * t1 111
89 * t2 222
90 * */
91
92 thread th3(t3);
93 thread th4(t4);
94
95 th3.join(); // 等待t3退出
96 th4.join(); // 等待t4退出
97 /* 输出结果: (全是 t3 的,如果不使用 mutex 就不会固定)
98 * 19 t3
99 * ... t3
100 * 0 t3
101 * */
102
103// th3.detach();
104// th4.detach();
105 /*
106 * 没有输出,主线程直接 return 0
107 * */
108
109 cout << "here is main\n\n";
110 return 0;
111}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected