使用函数对象初始化thread对象有两种方式:
- 使用初始化列表
- 多加一层小括号
使用函数对象的好处是可以将数据和函数封装起来。
#include <iostream>
#include <thread>
using namespace std;
class FUNC
{
public:
void operator()(){
cout << "class FUNC"<<endl;
}
};
int main()
{
thread t{FUNC()}; // 使用初始化列表
thread m((FUNC())); // 多使用一层括号
t.join();
FUNC f;
thread m(f);
m.join();
exit(0);
}