2021SC@SDUSC
我们看完线程并发:
void thread01()
{
for (int i = 0; i < 5; i++)
{
cout << "Thread 01 is working !" << endl;
Sleep(100);
}
}
void thread02()
{
for (int i = 0; i < 5; i++)
{
cout << "Thread 02 is working !" << endl;
Sleep(200);
}
}
int main()
{
thread task01(thread01);
thread task02(thread02);
task01.join();
task02.join();
for (int i = 0; i < 5; i++)
{
cout << "Main thread is working !" << endl;
Sleep(200);
}
system("pause");
}
join方式,等待启动的线程完成,才会继续往下执行。假如前面的代码使用这种方式。这是其中一个线程等待另一个线程的完成,之后才会向下进行。
再看另一种情况:
void thread01()
{
for (int i = 0; i < 5; i++)
{
cout << "Thread 01 is working !" <