C++多线程学习笔记08(future 、packaged_task 、promise)

一:std::async std::future 创建后台任务,并返回值
希望线程返回一个结果
std::async 是一个函数模板,用来启动一个异步任务;之后返回一个std::future对象,是个类模板。
“启动一个异步任务”,自动创建一个线程并开始执行对应的线程入口函数,返回std::future对象,里面含有线程入口函数所返回的结果。使用get()函数获取。
参数:
1)std::launch::deferred 表示线程入口函数调用被延迟到std::future 的 wait()或者 get()函数调用时才执行
如果wait() or get() 没调用,程序没执行,根本就没创建。
std::launch::deferred:延迟调用,并且没有创建新线程,只是在主线程中调用的线程入口函数

2)std:;launch::async ,在调用async函数的时候就开始创建线程
//系统默认使用此模式

二、std::packaged_task 打包任务,把任务包装起来
是类模板,模板参数是各种可调用对象。

三、std::promise,类模板,在某个线程赋值,又在另一个线程将值取处
//总结:通过promise保存一个值,在将来某个时刻通过把future绑定到这个promise上来,得到这个绑定的值

std::async与std::future 函数举例

#include "stdafx.h"
#include<future>
#include<thread>
#include<iostream>
using namespace std;
#include<vector>
int mythread(int mypar)
{
	cout << "mypar = " << mypar << endl;
	cout << "mythread() start "<<"threadid = "<< std::this_thread::get_id() << endl;
	std::chrono::milliseconds dura(5000);
	std::this_thread::sleep_for(dura);
	cout << "mythread() end " << "threadid = " << std::this_thread::get_id() << endl;
	return 5;
}

int main()
{
	cout << "main " << "threadid = " << std::this_thread::get_id() << endl;
	std::future<int> result = (mythread,15);  //创建一个线程开始执行,绑定关系,流程不卡在这
	cout << "contine...." << endl;
	int def = 0;
	cout << result.get() << endl;  //卡在这里等待mythread()执行完毕,拿到返回值  自能调用一次
	//result.wait();//等待线程返回,本身不返回结果
	cout << "结束" << endl;
	return 0;
}

main threadid = 8332
contine…
mypar = 15
mythread() start threadid = 1368
mythread() end threadid = 1368
5
结束
请按任意键继续. . .

std::async与std::future 类成员函数举例

// 线程7.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<future>
#include<thread>
#include<iostream>
using namespace std;
#include<vector>
class A
{
public:
	int mythread(int mypar)
	{
		cout << "mypar = " << mypar << endl;
		cout << "mythread() start " << "threadid = " << std::this_thread::get_id() << endl;
		std::chrono::milliseconds dura(5000);
		std::this_thread::sleep_for(dura);
		cout << "mythread() end " << "threadid = " << std::this_thread::get_id() << endl;
		return 5;
	}
};
int main()
{
	//类成员函数举例
	A a;
	int mypar = 12;
	cout << "main " << "threadid = " << std::this_thread::get_id() << endl;
	std::future<int> result = std::async(std::launch::deferred , &A::mythread, &a, mypar); //第二个参数是对象引用,才能保证线程里用的是同一个对象 
	cout << "contine...." << endl;
	int def = 0;
	cout << result.get() << endl;  //卡在这里等待mythread()执行完毕,拿到返回值  自能调用一次
	cout << "结束" << endl;
	return 0;
}

参数std::launch::deferred 结果显示
main threadid = 10388
contine…
mypar = 12
mythread() start threadid = 10388
mythread() end threadid = 10388
5
结束
请按任意键继续. . .
参数std::launch::async 结果显示
main threadid = 3020
contine…
mypar = 12
mythread() start threadid = 11148
mythread() end threadid = 11148
5
结束
请按任意键继续. . .

packaged_task用法

#include "stdafx.h"
#include<future>
#include<thread>
#include<iostream>
using namespace std;
#include<vector>

int main()
{
	//packaged_task用法
	cout << "main " << "threadid = " << std::this_thread::get_id() << endl;
	std::packaged_task<int(int)>mypt([](int mypar) {
		cout << "mypar = " << mypar << endl;
		cout << "mythread() start " << "threadid = " << std::this_thread::get_id() << endl;
		std::chrono::milliseconds dura(5000);
		std::this_thread::sleep_for(dura);
		cout << "mythread() end " << "threadid = " << std::this_thread::get_id() << endl;
		return 5;
	}); //我们把函数mythread通过packaged_task包装起来
	{
	std::thread t1(std::ref(mypt),12);
	t1.join();
	std::future<int> result = mypt.get_future();//std::future对象包含线程入口函数的返回结果,这里result 保存mythread的返回结果
	cout << result.get() << endl;
	}
	{//也可以这样
	//mypt(1314); // 相当于函数调用
	//std::future<int> result = mypt.get_future();
	//cout << result.get() << endl;
	}
	cout << "结束" << endl;
	return 0;
}

main threadid = 4924
mypar = 1314
mythread() start threadid = 4924
mythread() end threadid = 4924
5
结束
请按任意键继续. . .

packaged_task 容器移动

#include "stdafx.h"
#include<future>
#include<thread>
#include<iostream>
using namespace std;
#include<vector>
vector <std::packaged_task<int(int)>> mytask;
int main()
{
	//packaged_task用法
	cout << "main " << "threadid = " << std::this_thread::get_id() << endl;
	std::packaged_task<int(int)>mypt([](int mypar) {
		cout << "mypar = " << mypar << endl;
		cout << "mythread() start " << "threadid = " << std::this_thread::get_id() << endl;
		std::chrono::milliseconds dura(5000);
		std::this_thread::sleep_for(dura);
		cout << "mythread() end " << "threadid = " << std::this_thread::get_id() << endl;
		return 5;
	}); //我们把函数mythread通过packaged_task包装起来

	mytask.push_back(std::move(mypt));//入容器,这里用移动语义,入进去之后mypt为空
	std::packaged_task<int(int)>mypt2;
	auto iter = mytask.begin();
	mypt2 = std::move(*iter);//移动语义
	mytask.erase(iter);//删除第一个元素,后续不可使用iter
    mypt2(1314); // 相当于函数调用
	std::future<int> result = mypt2.get_future();
	cout << result.get() << endl;
	cout << "结束" << endl;
	return 0;
}
// 线程7.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<future>
#include<thread>
#include<iostream>
using namespace std;
#include<vector>
void mythread3(std::promise<int> &tmpp,int calc) 
{
	std::chrono::milliseconds dura(5000);
	std::this_thread::sleep_for(dura);
	calc++;
	calc *= 10;

	int result = calc;
	tmpp.set_value(result);
	return;
}

void mythread4(std::future<int> &tmpf)
{
	auto result = tmpf.get();
	cout << "mythread4 result = " << result << endl;
	return;
}

int main()
{
	//std::promise,类模板
	std::promise<int>myprom;//声明一个std::promise对象myprom,保存值类型为int
	std::thread t1(mythread3,std::ref(myprom),12);
	t1.join();
    //获取结果
	std::future<int> fu1 = myprom.get_future();//promise 与 future 绑定,用于获取线程返回值
	/*auto result = fu1.get();
	cout << "result = " << result << endl;*/

	std::thread t2(mythread4,std::ref(fu1));    
	t2.join();//等待thread4线程执行完毕
	cout << "结束" << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值