C++多线程 async,future, packaged_task , promise用法

1、async

async: 启动一个异步线程,可以配合future类来获得返回值

#include<iostream>
#include<thread>
#include<future>
using namespace std;


int mythread()
{
	cout << "mythread start,id= " << this_thread::get_id() << endl;
	this_thread::sleep_for(chrono::milliseconds(1000));  //睡眠1秒
	cout << "mythread end,id= " << this_thread::get_id() << endl;
	return 5;
}

int main()
{
	cout << "async ---test" << endl;
	future<int> result = async(mythread);
	//async 创建异步线程
	//future来绑定关系,获取线程返回值
	//其他操作
	//.....
	cout << "result=" << result.get() << endl << endl;   //阻塞在此处,直到获得线程返回结果
}

在这里插入图片描述

2、packaged_task

packaged_task: 包装线程函数,可以配合future类来获得返回值

#include<iostream>
#include<thread>
#include<future>
using namespace std;


int mythread()
{
	cout << "mythread start,id= " << this_thread::get_id() << endl;
	this_thread::sleep_for(chrono::milliseconds(1000));  //睡眠1秒
	cout << "mythread end,id= " << this_thread::get_id() << endl;
	return 5;
}
int main()
{
	cout << "packaged_task ---test" << endl;
	packaged_task<int()> mypt(mythread);    //把线程函数包装起来
	thread t1(std::ref(mypt));
	future<int> result = mypt.get_future();  //绑定线程未来返回值
	t1.detach();
	cout << "result=" << result.get() << endl << endl;  //阻塞等待
}

在这里插入图片描述

3、promise

promise: 配合future类来实现线程间通信

#include<iostream>
#include<thread>
#include<future>
using namespace std;

void mythread2(promise<int>& tmpp, int calc)
{
	cout << "mythread2 start,id= " << this_thread::get_id() << endl;
	int res = calc * 2;
	this_thread::sleep_for(chrono::milliseconds(2000));  //睡眠2秒
	cout << "mythread2 end,id= " << this_thread::get_id() << endl;
	tmpp.set_value(res);    //赋值,传递到线程外部
	return;
}

void mythread3(future<int>& ful)
{
	cout << "mythread3 start,id= " << this_thread::get_id() << endl;

	int x = ful.get();		//get()会阻塞等待另一个线程中赋值完毕,实现线程间通信

	cout << "mythread3_id= " << this_thread::get_id() << " ,ful.get()= " << x << endl;
	this_thread::sleep_for(chrono::milliseconds(1000));  //睡眠1秒
	cout << "mythread3 end,id= " << this_thread::get_id() << endl;
	return;
}
int main()
{

	cout << " promise---test" << endl;
	promise<int> myprom;     //用于线程间通信
	thread t2(mythread2, ref(myprom), 10);

	//线程间通信
	future<int> ful = myprom.get_future();   //绑定
	thread t3(mythread3, ref(ful));     //mythread3等待获取参数
	t2.join();
	t3.join();
	//auto result3 = ful.get();        //get只能调用一次
	//cout << "result3=" << result3 << endl << endl;
}

在这里插入图片描述

4、future_status

future_status: 可以判断线程状态,是否执行完毕

#include<iostream>
#include<thread>
#include<future>
using namespace std;

int mythread()
{
	cout << "mythread start,id= " << this_thread::get_id() << endl;
	this_thread::sleep_for(chrono::milliseconds(3000));  //睡眠3秒
	cout << "mythread end,id= " << this_thread::get_id() << endl;
	return 5;
}
int main()
{

	future<int> result = async(mythread);    //启动异步线程,绑定返回值(result)
	//future<int> result = async(launch::deferred,mythread);  //此时并不启动,延时启动,result.get()时启动

	future_status status = result.wait_for(chrono::seconds(5));   //等5秒后判断result的状态
	if (status == future_status::timeout)  //超时,wait_for后线程没有结束
	{
		cout << "线程未执行完" << endl;
	}
	else if (status == future_status::ready)  //线程已执行完毕,返回值已准备好
	{
		//本例中等待5s,线程只睡眠3s,所以是ready状态
		cout << "线程执行完毕" << endl;
		cout << "result = " << result.get() << endl;
	}
	else if (status == future_status::deferred)    //线程延时启动,需设置launch::deferred标志位
	{
		cout << "线程延时执行" << endl;
		cout << "result = " << result.get() << endl;
	}
}

在这里插入图片描述

5、shared_future

shared_future: future.get()是移动语义,只能调用一次,shared_future则可以多次调用get,实现多线程通信

#include<iostream>
#include<thread>
#include<future>
using namespace std;

void mythread2(promise<int>& tmpp, int calc)
{
	cout << "mythread2 start,id= " << this_thread::get_id() << endl;
	int res = calc * 2;
	this_thread::sleep_for(chrono::milliseconds(3000));  //睡眠1秒
	cout << "mythread2 end,id= " << this_thread::get_id() << endl;
	tmpp.set_value(res);    //赋值,传递到线程外部
	return;
}

void mythread3(shared_future<int>& ful)
{
	cout << "mythread3 start,id= " << this_thread::get_id() << endl;
	int x = ful.get();   //get()会阻塞等待另一个线程中赋值完毕,实现线程间通信
	cout << "mythread3_id= " << this_thread::get_id() << " ,ful.get()= " << x << endl;
	this_thread::sleep_for(chrono::milliseconds(1000));  //睡眠1秒
	cout << "mythread3 end,id= " << this_thread::get_id() << endl;
	return;
}

void mythread4(shared_future<int>& ful)
{
	cout << "mythread4 start,id= " << this_thread::get_id() << endl;
	int x = ful.get();   //get()会阻塞等待另一个线程中赋值完毕,实现线程间通信
	cout << "mythread4_id= " << this_thread::get_id() << " ,ful.get()= " << x << endl;
	this_thread::sleep_for(chrono::milliseconds(1000));  //睡眠1秒
	cout << "mythread4 end,id= " << this_thread::get_id() << endl;
	return;
}
int main()
{
	cout << " promise---test" << endl;
	promise<int> myprom;     //用于 线程间通信
	thread t2(mythread2, ref(myprom), 10);
	t2.detach();

	//获取结果值
	shared_future<int> ful = myprom.get_future();   //绑定
	thread t3(mythread3, ref(ful));     //mythread3等待获取参数
	thread t4(mythread4, ref(ful));     //mythread4等待获取参数
	t3.join();
	t4.join();
	//auto result3 = ful.get();        //get只能调用一次
	//cout << "result3=" << result3 << endl << endl;
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值