1 . 无参线程
// 线程管理
#include <iostream>
#include <thread>
void hello(){
std::cout<<"Hello Concurrent World!"<<endl;
}
// 众所周知,主线程的入口函数为main函数。同样的,子线程也需要有一个入口函数hello,
// 并在调用子线程的构造函数的时候传给子线程std::thread t(hello);
int main(){
std::thread t(hello); // 子线程在创建那一刻就启动了 创建/启动一个新线程 无参线程
t.join(); // 我们希望子线程输出字符串的时候,主线程即使运行完也要等待它,两个线程一起
// 结束,所以调用了join方法
}
此外,这个函数hello可以通过重载Function call 实现成仿函数的形式:
class hello{
public:
void operator()(){
std::cout<<"Hello Concurrent World!"<<std::endl;
}
};