c++ 多线程学习笔记(二)

来源:互联网 发布:java随机生成二维数组 编辑:程序博客网 时间:2024/05/22 06:17

C++ thread

#include <iostream>#include <thread>#include <Windows.h>using namespace std;int totalNum = 100;void thread01(){    while (totalNum > 0)    {        cout << totalNum << endl;        totalNum--;        Sleep(100);    }}void thread02(){    while (totalNum > 0)    {        cout << totalNum << endl;        totalNum--;        Sleep(100);    }}int main(){    thread task01(thread01);    thread task02(thread02);    task01.detach(); //join函数会阻塞主线程    task02.detach();//使用detach的主线程和两个子线程并行执行    system("pause");}