#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<thread>
using namespace std;
//functional object
class Nick {
public:
void operator()() {
cout << "void operator()(),my thread id:" << this_thread::get_id() << endl;
}
};
class Bobo {
public:
void operator()(int limit) {
cout << "class Bobo,thread id:" << this_thread::get_id() << endl;
for (int i = 0; i < limit; i++) {
cout << "class Bobo:" << i << endl;
}
}
};
/*
class Judy {
public:
void operator()()(string &str) {
cout << "字符串:" << str << endl;
}
};
*/
int main(){
Nick nick;
nick();
nick.operator()();
cout << endl;
thread thread1 = thread(Nick());
thread1.join();
cout << "void operator()(),main thread id:" << this_thread::get_id() << endl << endl;
thread thread2 = thread(Bobo(), 10);
thread2.join();
for (int i = 0; i < 10; i++) {
cout << "main:" << i << endl;
}
system("pause");
return 0;
}
C++11采用Callable接口创建线程
最新推荐文章于 2024-04-12 21:29:09 发布