引用头文件:
#include <thread>
实现线程执行函数
//线程执行函数
void *thread_func(void * arg)
{
for (int i = 0; i < *(int*)arg; ++i) {
std::cout<<"子线程输出:"<<i+1<<std::endl;
}
}
3.创建并启动线程
int count=35;
//创建线程
std::thread t{thread_func,(void*)&count};
t.detach();//分离线程
完整源码: