配合B站视频看添加链接描述
1. 并发、进程、线程概念:
在这里插入图片描述
2 创建线程:
普通函数方法创建线程
包含头文件,调用thread类创建一个线程对象;
#include <thread>
#include <iostream>
using namespace std;
void print(){
cout<<"子线程"<<endl;
}
int main(){
thread test1(print);
cout<<"主线程"<<endl;
return 0;
}
此时,执行代码会出错,会调用abort 函数终止程序。
需要使用join 函数,详解join 和detach函数,汇合线程,阻塞主线程。
#include <thread>
#include <iostream>
#include <windows.h>
using namespace std;
void print(){
Sleep(5000);//5m
cout<<"子线程"<<endl;
}
int main(){
thread test1(print);