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

輸出:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章