8.async,future, packaged_task, promise

C++11 多线程std:: async与std::thread的区别

一.std::async, std::future创建后台任务并返回值

普通函数形式

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;

int mythread() {//线程入口函数
	cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

	std::chrono::microseconds dura(5000);//sleep 5s
	std::this_thread::sleep_for(dura);//休息一定时长

	cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
	return 5;
}

int main()
{
	
	//一:std::async, std::future创建后台任务并返回值
	/*
		std::async是个函数模板,用来启动一个异步任务,启动一个异步任务之后,他返回一个std::future对象,std::future是个类模板
		什么叫“启动一个异步任务”:就是自动创建一个线程并开始执行对应的线程入口函数,他返回一个std::future对象,
			这个std::future对象里边就含有线程入口函数所返回的结果(线程返回的结果),我们可以通过调用future对象的成员函数get()来获取结果;
			future:将来的意思,有人也称呼std::future提供了一种访问异步操作结果的机制,就是说这个结果你可能不能马上得到,
			在线程执行完毕的时候,你就能拿到结果了。所以大家就这么理解:这个future(对象)里会保存一个值,在将来的某个时刻可以拿到
	
	*/
	//下列程序通过std::future对象的get()成员函数等待线程执行结束并返回结果
	//这个get()函数很牛,不拿到将来的返回值誓不罢休,不拿到值我就卡在这等待拿值
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::future<int>result = std::async(mythread);//创建一个线程并开始执行,绑定关系
	cout << "continue....." << endl;
	int def = 0;
	cout << result.get() << endl;//卡在这里等待mythread()执行完毕,拿到结果,get()只能调用一次
	//result.wait();//等待线程返回,本身并不返回结果
	cout << "hello world" << endl;


	return 0;
}
/*
output:

main thread id = 15060
continue.....
mythread() start, thread id = 11996
mythread() end, thread id = 11996
5
hello world

*/



类成员函数形式

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;


class A {
public:
	int mythread(int mypar) {//线程入口函数

		cout << mypar << endl;
		cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

		std::chrono::microseconds dura(5000);//sleep 5s
		std::this_thread::sleep_for(dura);//休息一定时长

		cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
		return 5;
	}
};


int main()
{
	
	//一:std::async, std::future创建后台任务并返回值
	/*
		std::async是个函数模板,用来启动一个异步任务,启动一个异步任务之后,他返回一个std::future对象,std::future是个类模板
		什么叫“启动一个异步任务”:就是自动创建一个线程并开始执行对应的线程入口函数,他返回一个std::future对象,
			这个std::future对象里边就含有线程入口函数所返回的结果(线程返回的结果),我们可以通过调用future对象的成员函数get()来获取结果;
			future:将来的意思,有人也称呼std::future提供了一种访问异步操作结果的机制,就是说这个结果你可能不能马上得到,
			在线程执行完毕的时候,你就能拿到结果了。所以大家就这么理解:这个future(对象)里会保存一个值,在将来的某个时刻可以拿到
	
	*/
	//下列程序通过std::future对象的get()成员函数等待线程执行结束并返回结果
	//这个get()函数很牛,不拿到将来的返回值誓不罢休,不拿到值我就卡在这等待拿值
	A a;
	int tmppar = 12;
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::future<int>result = std::async(&A::mythread,&a,tmppar);//创建一个线程并开始执行,绑定关系
	cout << "continue....." << endl;
	int def = 0;
	cout << result.get() << endl;//卡在这里等待mythread()执行完毕,拿到结果,get()只能调用一次
	//result.wait();//等待线程返回,本身并不返回结果
	cout << "hello world" << endl;


	return 0;
}


/*
output:
	main thread id = 13876
	continue.....
	12
	mythread() start, thread id = 2488
	mythread() end, thread id = 2488
	5
	hello world

*/

std::launch::deferred

我们通过额外向std::async()传递一个参数,该参数的类型是std::launch类型(枚举类型),来达到一些特殊的目的;
a)std::launch::defered表示线程入口函数调用被延迟到std::future的wait()或者get()函数调用时才执行;
那么,如果wait()或者get()没有调用,线程会执行吗?没执行,实际上线程根本没创建!
std::launch::deferred延迟调用,并且没有创建新线程,是在主线程中调用的线程入口函数!

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;


class A {
public:
	int mythread(int mypar) {//线程入口函数

		cout << mypar << endl;
		cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

		std::chrono::microseconds dura(5000);//sleep 5s
		std::this_thread::sleep_for(dura);//休息一定时长

		cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
		return 5;
	}
};


int main()
{
	
	//一:std::async, std::future创建后台任务并返回值
	/*
		std::async是个函数模板,用来启动一个异步任务,启动一个异步任务之后,他返回一个std::future对象,std::future是个类模板
		什么叫“启动一个异步任务”:就是自动创建一个线程并开始执行对应的线程入口函数,他返回一个std::future对象,
			这个std::future对象里边就含有线程入口函数所返回的结果(线程返回的结果),我们可以通过调用future对象的成员函数get()来获取结果;
			future:将来的意思,有人也称呼std::future提供了一种访问异步操作结果的机制,就是说这个结果你可能不能马上得到,
			在线程执行完毕的时候,你就能拿到结果了。所以大家就这么理解:这个future(对象)里会保存一个值,在将来的某个时刻可以拿到
	
	*/
	//下列程序通过std::future对象的get()成员函数等待线程执行结束并返回结果
	//这个get()函数很牛,不拿到将来的返回值誓不罢休,不拿到值我就卡在这等待拿值
	A a;
	int tmppar = 12;
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::future<int>result = std::async(std::launch::deferred,&A::mythread,&a,tmppar);//创建一个线程并开始执行,绑定关系
	cout << "continue....." << endl;
	int def = 0;
	cout << result.get() << endl;//卡在这里等待mythread()执行完毕,拿到结果,get()只能调用一次
	//result.wait();//等待线程返回,本身并不返回结果
	cout << "hello world" << endl;


	return 0;
}


/*
output:
    发现主线程和子线程id号都一样,根本没创建子线程!

	main thread id = 13216
	continue.....
	12
	mythread() start, thread id = 13216
	mythread() end, thread id = 13216
	5
	hello world

*/

这里注释掉cout << result.get() << endl看看

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;


class A {
public:
	int mythread(int mypar) {//线程入口函数

		cout << mypar << endl;
		cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

		std::chrono::microseconds dura(5000);//sleep 5s
		std::this_thread::sleep_for(dura);//休息一定时长

		cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
		return 5;
	}
};


int main()
{
	
	//一:std::async, std::future创建后台任务并返回值
	/*
		std::async是个函数模板,用来启动一个异步任务,启动一个异步任务之后,他返回一个std::future对象,std::future是个类模板
		什么叫“启动一个异步任务”:就是自动创建一个线程并开始执行对应的线程入口函数,他返回一个std::future对象,
			这个std::future对象里边就含有线程入口函数所返回的结果(线程返回的结果),我们可以通过调用future对象的成员函数get()来获取结果;
			future:将来的意思,有人也称呼std::future提供了一种访问异步操作结果的机制,就是说这个结果你可能不能马上得到,
			在线程执行完毕的时候,你就能拿到结果了。所以大家就这么理解:这个future(对象)里会保存一个值,在将来的某个时刻可以拿到
	
	*/
	//下列程序通过std::future对象的get()成员函数等待线程执行结束并返回结果
	//这个get()函数很牛,不拿到将来的返回值誓不罢休,不拿到值我就卡在这等待拿值
	A a;
	int tmppar = 12;
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::future<int>result = std::async(std::launch::deferred,&A::mythread,&a,tmppar);//创建一个线程并开始执行,绑定关系
	cout << "continue....." << endl;
	int def = 0;
	//cout << result.get() << endl;//卡在这里等待mythread()执行完毕,拿到结果,get()只能调用一次
	//result.wait();//等待线程返回,本身并不返回结果
	cout << "hello world" << endl;


	return 0;
}


/*
output:
    发现主线程和子线程id号都一样,根本没创建子线程!

	main thread id = 9776
	continue.....
	hello world

*/

std::launch::async

在调用async函数的时候就开始创建线程

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;


class A {
public:
	int mythread(int mypar) {//线程入口函数

		cout << mypar << endl;
		cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

		std::chrono::microseconds dura(5000);//sleep 5s
		std::this_thread::sleep_for(dura);//休息一定时长

		cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
		return 5;
	}
};


int main()
{
	
	//一:std::async, std::future创建后台任务并返回值
	/*
		std::async是个函数模板,用来启动一个异步任务,启动一个异步任务之后,他返回一个std::future对象,std::future是个类模板
		什么叫“启动一个异步任务”:就是自动创建一个线程并开始执行对应的线程入口函数,他返回一个std::future对象,
			这个std::future对象里边就含有线程入口函数所返回的结果(线程返回的结果),我们可以通过调用future对象的成员函数get()来获取结果;
			future:将来的意思,有人也称呼std::future提供了一种访问异步操作结果的机制,就是说这个结果你可能不能马上得到,
			在线程执行完毕的时候,你就能拿到结果了。所以大家就这么理解:这个future(对象)里会保存一个值,在将来的某个时刻可以拿到
	
	*/
	//下列程序通过std::future对象的get()成员函数等待线程执行结束并返回结果
	//这个get()函数很牛,不拿到将来的返回值誓不罢休,不拿到值我就卡在这等待拿值
	A a;
	int tmppar = 12;
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::future<int>result = std::async(std::launch::async,&A::mythread,&a,tmppar);//创建一个线程并开始执行,绑定关系
	cout << "continue....." << endl;
	int def = 0;
	cout << result.get() << endl;//卡在这里等待mythread()执行完毕,拿到结果,get()只能调用一次
	//result.wait();//等待线程返回,本身并不返回结果
	cout << "hello world" << endl;


	return 0;
}


/*
output:
    main thread id = 14644
	continue.....
	12
	mythread() start, thread id = 10496
	mythread() end, thread id = 10496
	5
	hello world


*/

std::launch::acync|std::launch::deferred

两种情况都有可能,默认就是这种(不手动传递任何一种)

二.std::packaged_task

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;

int mythread(int mypar) {//线程入口函数

		cout << mypar << endl;
		cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

		std::chrono::microseconds dura(5000);//sleep 5s
		std::this_thread::sleep_for(dura);//休息一定时长

		cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
		return 5;
}

int main()
{
	/*
		二.std::packaged_task:打包任务,把任务包装起来
		是个类模板,他的模板参数是各种可调用对象;通过std::packaged_task来把各种可调用对象包装起来,方便将来作为线程入口函数来调用
		
	*/
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::packaged_task<int(int)>mypt(mythread);//我们把函数mythread通过packaged_task包装起来
	std::thread t1(std::ref(mypt),1);//线程直接开始执行。第二个参数作为线程入口函数的参数。
	t1.join();//等待线程执行完毕
	std::future<int>result = mypt.get_future();//std::future对象里包含有线程入口函数的返回结果,这里result保存mythread返回的结果
	cout << result.get() << endl;
	cout << "hello world" << endl;
	return 0;
}


/*
output:
   main thread id = 2084
	1
	mythread() start, thread id = 11244
	mythread() end, thread id = 11244
	5
	hello world



*/

用lambda表达式作为线程入口函数

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;

int mythread(int mypar) {//线程入口函数

		cout << mypar << endl;
		cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

		std::chrono::microseconds dura(5000);//sleep 5s
		std::this_thread::sleep_for(dura);//休息一定时长

		cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
		return 5;
}

int main()
{
	/*
		二.std::packaged_task:打包任务,把任务包装起来
		是个类模板,他的模板参数是各种可调用对象;通过std::packaged_task来把各种可调用对象包装起来,方便将来作为线程入口函数来调用
		
	*/
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::packaged_task<int(int)>mypt([](int mypar) {
		
			cout << mypar << endl;
			cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

			std::chrono::microseconds dura(5000);//sleep 5s
			std::this_thread::sleep_for(dura);//休息一定时长

			cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
			return 5;
		
		});//我们把函数mythread通过packaged_task包装起来
	std::thread t1(std::ref(mypt),1);//线程直接开始执行。第二个参数作为线程入口函数的参数。
	t1.join();//等待线程执行完毕
	std::future<int>result = mypt.get_future();//std::future对象里包含有线程入口函数的返回结果,这里result保存mythread返回的结果
	cout << result.get() << endl;
	cout << "hello world" << endl;
	return 0;
}


/*
output:
   main thread id = 2084
	1
	mythread() start, thread id = 11244
	mythread() end, thread id = 11244
	5
	hello world

*/

用packaged_task包装起来的可调用对象还可以直接调用,所以从这个角度看,packaged_task对象也是一个可调用对象

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;

int main()
{
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::packaged_task<int(int)>mypt([](int mypar) {
		
			cout << mypar << endl;
			cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

			std::chrono::microseconds dura(5000);//sleep 5s
			std::this_thread::sleep_for(dura);//休息一定时长

			cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
			return 5;
		
		});//我们把函数mythread通过packaged_task包装起来
	//std::thread t1(std::ref(mypt),1);//线程直接开始执行。第二个参数作为线程入口函数的参数。
	mypt(1);//直接调用,没有创建新线程
	//t1.join();//等待线程执行完毕
	std::future<int>result = mypt.get_future();//std::future对象里包含有线程入口函数的返回结果,这里result保存mythread返回的结果
	cout << result.get() << endl;
	cout << "hello world" << endl;
	return 0;
}


/*
output:

相当于函数调用,没有创建线程
	main thread id = 14176
	1
	mythread() start, thread id = 14176
	mythread() end, thread id = 14176
	5
	hello world


*/

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;

vector<std::packaged_task<int(int)>>mytasks;//容器
int main()
{
	/*
		二.std::packaged_task:打包任务,把任务包装起来
		是个类模板,他的模板参数是各种可调用对象;通过std::packaged_task来把各种可调用对象包装起来,方便将来作为线程入口函数来调用
		
	*/
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::packaged_task<int(int)>mypt([](int mypar) {
		
			cout << mypar << endl;
			cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

			std::chrono::microseconds dura(5000);//sleep 5s
			std::this_thread::sleep_for(dura);//休息一定时长

			cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
			return 5;
		
		});//我们把函数mythread通过packaged_task包装起来

	mytasks.push_back(std::move(mypt));//容器,移动语义,入进去之后mypt就为空!

	//.....
	std::packaged_task<int(int)>mypt2;
	vector<std::packaged_task<int(int)>>::iterator iter = mytasks.begin();
	mypt2 = std::move(*iter);//移动语义

	mytasks.erase(iter);//删除第一个元素,迭代已经失效,所以后续代码不可以继续使用iter
	mypt2(123);

	
	std::future<int>result = mypt2.get_future();//std::future对象里包含有线程入口函数的返回结果,这里result保存mythread返回的结果
	cout << result.get() << endl;
	cout << "hello world" << endl;
	return 0;
}


/*
output:

相当于函数调用,没有创建线程
	main thread id = 9932
	123
	mythread() start, thread id = 9932
	mythread() end, thread id = 9932
	5
	hello world
*/

三.std::promise

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;

void mythread(std::promise<int>& temp,int calc) {//大家注意第一个参数
	//一系列复杂运算
	//做其他运算,比如花了整整5秒
	std::chrono::milliseconds dura(5000);
	std::this_thread::sleep_for(dura);
	//计算完成了
	int result = calc;//保存结果
	temp.set_value(result);//结果我保存到了temp这个对象中;

}

void mythread2(std::future<int>&temp) {
	auto result = temp.get();
	cout << "mythread2 result = " << result << endl;
}
int main()
{
	/*
		三.std::promise:
		是个类模板,我们能够在某个线程中给他赋值,然后我们可以在其他线程中把这个值取出来用
		总结:通过promise保存一个值,在将来某个时刻我们通过把一个future绑定到这个promise上来得到这个绑定值
		
	*/

	std::promise<int>myprom;//声明一个std::promise对象,保存的值类型为int
	std::thread t1(mythread, std::ref(myprom), 180);
	t1.join();

	//获取结果值
	std::future<int>fu1 = myprom.get_future();//promise和future绑定,用于获取线程返回值

	std::thread t2(mythread2, std::ref(fu1));
	t2.join();

	
	cout << "hello world" << endl;
	return 0;
}


/*
output:

mythread2 result = 180
hello world



*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值