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();
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。