C++多线程(1-入门)

#include <thread>
#include <iostream>

using namespace std;

void ThreadMain()
{
    cout<<"begin subthread main"<<this_thread::get_id()<<endl;
    for(int i =0; i<10; i++)
    {
     cout<<"in thread"<<i<<endl; this_thread::sleep_for(chrono::seconds(
1)); } cout<<"end sub thread main"<<this_thread::get_id()<<endl; } int main(int argc, char*argv[]) { cout<<"main thread ID"<<this_thread::get_id()<<endl; //线程创建启动 thread th(ThreadMain); cout<<"begin wait sub thread"<<endl; //阻塞等待子线程退出 th.join(); cout<<"end wait sub thread"<<endl; return 0;
}
posted @ 2022-07-18 17:07  二先生-  阅读(25)  评论(0)    收藏  举报