1, 写一个函数,函数的参数是函数对象及参数,功能和thread类的构造函数相同
#include <iostream>
#include <thread>
#include <functional>
using namespace std;
void show0() { // 普通函数。
cout << "亲爱的,我是一只傻傻鸟。\n";
}
void show1(const string& message) { // 普通函数。
cout << "亲爱的," << message << endl;
}
struct CC // 类中有普通成员函数。
{
void show2(int bh, const string& message) {
cout << "亲爱的" << bh << "号," << message << endl;
}
};
template<typename Fn, typename...Args>
auto show(Fn&& fn, Args&&...args) -> decltype(bind(forward<Fn>(fn), forward<Args>(args)...))
{
cout << "表白前的准备工作........................................\n";
auto f = bind(forward<Fn>(fn), forward<Args>(args)...);
f();
cout << "表白完成。\n";
return f;
}
int main()
{
show(show0);
show(show1,"我是一只傻傻鸟。");
CC cc;
auto f = show(&CC::show2,&cc, 3,"我是一只傻傻鸟。");
f();
//thread t1(show0);
//thread t2(show1,"我是一只傻傻鸟。");
//CC cc;
//thread t3(&CC::show2,&cc, 3,"我是一只傻傻鸟。");
//t1.join();
//t2.join();
//t3.join();
}