c++多线程系列
c++多线程thread操作(五)unique_lock加锁
c++多线程thread操作(七)父进程获取子进程变量的结果
(终)c++多线程thread操作(十)多线程并行实现数据相加的和
thread可调用的方式如下:
#include <iostream>
#include <thread>
using namespace std;
class A {
public:
void f(int x, char c);
int operator()(int m) { return m*m; }
};
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从主线程移动到子线程,在主线程中a已经失效了
thread t4(A(), 6);//传递临时创建的a对象给子线程
thread t5(foo, 6);//传递函数
thread t6([](int x) {return x*x; }, 6);//传递lambda函数
thread t7(&A::f, a,8 ,'w');//传递a的拷贝成员函数给子线程
thread t8(&A::f, &a, 8, 'w');//传递a的地址成员函数给子线程
async(a,6);//同样适用于上述的8种方式
return 0;
}