C++线程的几种调用方式

来源:互联网 发布:淘宝商品质量问题定义 编辑:程序博客网 时间:2024/05/29 19:47
#include<thread>#include<future>using namespace std;class A{public:void f(int x,char c){}int operator()(int N) { return 0; }};void foo(int x){}int main(){A a;thread t1(a, 6);  //传递a的拷贝给子线程thread t2(ref(a), 6); //传递a的引用给子线程thread t3(move(a), 6);//a在主线程中将不再有效thread t4(A(), 6);  //传递临时创建的a对象给子线程thread t5(foo, 6);  // 声明的函数:foothread t6([](int x) {return x*x; }, 6); // lambda函数thread t7(&A::f, a, 8, 'w'); //传递a的拷贝的成员函数给子线程   8和'w'是f()的参数 thread t8(&A::f, &a, 8, 'w'); //传递a的地址的成员函数给子线程 //async同样适用于以上八种方法async(launch::async, a, 6);     return 0;}

原创粉丝点击