C++11异步操作(二)

1、async

        async是一个函数模板,用来启动一个异步任务,启动异步任务后,返回一个future对象。这个future对象里含有线程入口函数锁返回的结果(线程返回结果),可通过future对象的成员函数get()来获取。

 

2、future

(1)future:

       将来的意思,提供了一种访问异步操作结果的机制,此结果可能没马上拿到,可在线程执行完毕后拿到结果,该结果可使用future类中get()函数来获取。

(2)launch::deferred和launch::async

       launch::deferred:表示线程入口函数调用被延迟到future的wait()或者get()调用时才执行 若wait()或get()没调用,那么线程是不执行的,并且没创建新线程,是在主线程中调用线程入口函数。

       launch::async:在调用async函数时立刻开始创建线程async()函数默认使用launch::async,所以一般省略此参数。

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <future>

using namespace std;

class A
{
public:
	A() {}
	~A() {}

	int myThread(int tm)
	{
		cout << "tm = " << tm << endl;
		cout << "myThread() start, threadID = " << this_thread::get_id() << endl;
		chrono::microseconds dura(5000);	// 定义时长,5秒
		this_thread::sleep_for(dura);		// 休眠一定时长
		cout << "myThread() end, threadID = " << this_thread::get_id() << endl;
		return 5;
	}

};


int main()
{
	A a;
	int tm = 122;
	cout << "main ,threadID = " << this_thread::get_id() << endl;
	future<int> result = async(&A::myThread, a, tm);                          // 马上调用
	// future<int> result = async(launch::async , &A::myThread, a, tm);
	// future<int> result = async(launch::deferred , &A::myThread, a, tm);    // 延迟调用

	cout << "continue...!" << endl;
	int def;
	def = 0;
	cout << "result.get() = "<< result.get() << endl;    // 流程卡在此等待线程返回,get()只能调一次不能多次调用,如果不等待获取可能会执行混乱
	// result.wait();                                    // 等待线程返回,本身并不返回结果
	cout << "end" << endl;
	
	system("pause");
	return 0;
}

输出:

 

3、packaged_task

       packaged_task:打包任务,把任务包装起来,是一个类模板,模板参数是各种可调用对象,通过packaged_task来把各种可执行对象包装起来,方便将来作为线程入。packaged_task包装起来的可调用对象也可直接调用。

(1)使用普通函数

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <future>

using namespace std;

int myThread(int tm)
{
	cout << "tm = " << tm << endl;
	cout << "myThread() start, threadID = " << this_thread::get_id() << endl;
	chrono::microseconds dura(5000);	// 定义时长,5秒
	this_thread::sleep_for(dura);		// 休眠一定时长
	cout << "myThread() end, threadID = " << this_thread::get_id() << endl;
	return 5;
}
int main()
{
	cout << "main ,threadID = " << this_thread::get_id() << endl;
	packaged_task<int(int)> myPt(myThread);		// 将mymyThread通过packaged_task包装起来,后续可直接使用包装起来的对象
	thread t1(ref(myPt), 1);                        // 线程直接开始执行,第二个参数作为线程入口函数参数
	t1.join();                                      // 等待线程执行结束
	future<int> result = myPt.get_future();		// furure对象里含线程入口函数的返回值,此处result保存的是myThread的返回值
	cout << "result = " << result.get() << endl;
	cout << "end" << endl;

	system("pause");
	return 0;
}

输出:

(2)使用lambda表达式:

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <future>

using namespace std;

int main()
{
packaged_task<int(int)> myPt([](int tm) {
		cout << "tm = " << tm << endl;
		cout << "myThread() start, threadID = " << this_thread::get_id() << endl;
		chrono::microseconds dura(5000);	// 定义时长,5秒
		this_thread::sleep_for(dura);		// 休眠一定时长
		cout << "myThread() end, threadID = " << this_thread::get_id() << endl;
		return 5;
	});
	thread t1(ref(myPt), 1);                     // 线程直接开始执行,第二个参数作为线程入口函数参数
	t1.join();                                  // 等待线程执行结束
	future<int> result = myPt.get_future();     // furure对象里含线程入口函数的返回值,此处result保存的是myThread的返回值
	cout << "result = " << result.get() << endl;
	cout << "end" << endl;

	// 直接调用
	//myPt(66);
	//future<int> result2 = myPt.get_future();
	//cout << "result2 = " << result2.get() << endl;

	system("pause");
	return 0;
}

(3)使用容器:

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <future>

using namespace std;

vector <packaged_task<int(int)>> myTask;

int main()
{
    packaged_task<int(int)> myPt([](int tm) {
		cout << "tm = " << tm << endl;
		cout << "myThread() start, threadID = " << this_thread::get_id() << endl;
		chrono::microseconds dura(5000);	// 定义时长,5秒
		this_thread::sleep_for(dura);		// 休眠一定时长
		cout << "myThread() end, threadID = " << this_thread::get_id() << endl;
		return 5;
	});

    cout << "main ,threadID = " << this_thread::get_id() << endl;
	myTask.push_back(move(myPt));
	packaged_task<int(int)> myPt2;
	auto it = myTask.begin();
	myPt2 = move(*it);
	myTask.erase(it);	// 删除第一个元素,迭代器已实现,故后续代码不能再使用it
	myPt2(123);
	future<int> result3 = myPt2.get_future();
	cout << "result3 = " << result3.get() << endl;

	system("pause");
	return 0;
}

输出:

 

4、promise

       promise:类模板,可在某个线程中给它赋值,然后在其他线程中,把这个值取出来用,可用于线程之间的数据传递。具体做法是通过promise保存一个值,在将来某时刻通过把一个future绑定到这个promise上,以获取promise上绑定的值

例:

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <future>

using namespace std;

void myThread(promise<int> &tmp, int calc)
{
	// 做一系列复杂操作
	calc++;
	calc *= 10;

	chrono::microseconds dura(5000);	// 定义时长,5秒
	this_thread::sleep_for(dura);		// 休眠一定时长
	
	int result = calc;
	tmp.set_value(result);	// 结果保存在tmp中
	cout << "myThread1 result = " << result << endl;
}

void myThread2(future<int> &tm)
{
	auto result = tm.get();
	cout << "myThread2 result = " << result << endl;
}

int main()
{
	promise<int> myPro;
	thread t1(myThread, ref(myPro), 99);
	t1.join();

	// 获取结果值
	future<int> fut = myPro.get_future();		// promise和future绑定,用于获取线程返回值
	// auto result = fut.get();					// get()只能调用一次
	// cout << "result = " << result << endl;
	
	thread t2(myThread2, ref(fut));
	t2.join();

	cout << "end" << endl;

	system("pause");
	return 0;
}

输出:

 

原创文章 99 获赞 68 访问量 3万+

猜你喜欢

转载自blog.csdn.net/King_weng/article/details/103834433