c++多线程thread操作(九)可调用方式

c++多线程系列

c++多线程thread操作(一)环境部署

c++多线程thread操作(二)线程管理

c++多线程thread操作(三)数据竞争互斥访问

c++多线程thread操作(四)死锁的问题和解决

c++多线程thread操作(五)unique_lock加锁

c++多线程thread操作(六)条件变量的使用

c++多线程thread操作(七)父进程获取子进程变量的结果

c++多线程thread操作(八)父线程传递数据给子线程

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;
}

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值