c++11多線程編程之std::async的介紹與實(shí)例
本節(jié)討論下在C++11中怎樣使用std::async來執(zhí)行異步task。
C++11中引入了std::async
什么是std::async
std::async()是一個(gè)接受回調(diào)(函數(shù)或函數(shù)對象)作為參數(shù)的函數(shù)模板,并有可能異步執(zhí)行它們.
template<class Fn, class... Args> future<typename result_of<Fn(Args...)>::type> async(launch policy, Fn&& fn, Args&&...args);
std::async返回一個(gè) std::future<T>,它存儲(chǔ)由 std::async()執(zhí)行的函數(shù)對象返回的值。
函數(shù)期望的參數(shù)可以作為函數(shù)指針參數(shù)后面的參數(shù)傳遞給std::async()。
std::async中的第一個(gè)參數(shù)是啟動(dòng)策略,它控制std::async的異步行為,我們可以用三種不同的啟動(dòng)策略來創(chuàng)建std::async
·std::launch::async
保證異步行為,即傳遞函數(shù)將在單獨(dú)的線程中執(zhí)行
·std::launch::deferred
當(dāng)其他線程調(diào)用get()來訪問共享狀態(tài)時(shí),將調(diào)用非異步行為
·std::launch::async | std::launch::deferred
默認(rèn)行為。有了這個(gè)啟動(dòng)策略,它可以異步運(yùn)行或不運(yùn)行,這取決于系統(tǒng)的負(fù)載,但我們無法控制它。
如果我們不指定一個(gè)啟動(dòng)策略,其行為將類似于std::launch::async | std::launch::deferred
本節(jié)我們將使用std::launch::async啟動(dòng)策略
我們可以在std::async傳遞任何回調(diào),如:
·函數(shù)指針
·函數(shù)對象
·lambda表達(dá)式
std::async的需求
假設(shè)我們必須從數(shù)據(jù)庫和文件系統(tǒng)里里獲取一些數(shù)據(jù)(字符串),然后需要合并字符串并打印。
在單線程中,我們這樣做:
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
using namespace std::chrono;
std::string fetchDataFromDB(std::string recvData) {
//確保函數(shù)要5秒才能執(zhí)行完成
std::this_thread::sleep_for(seconds(5));
//處理創(chuàng)建數(shù)據(jù)庫連接、獲取數(shù)據(jù)等事情
return "DB_" + recvData;
}
std::string fetchDataFromFile(std::string recvData) {
//確保函數(shù)要5秒才能執(zhí)行完成
std::this_thread::sleep_for(seconds(5));
//處理獲取文件數(shù)據(jù)
return "File_" + recvData;
}
int main() {
//獲取開始時(shí)間
system_clock::time_point start = system_clock::now();
//從數(shù)據(jù)庫獲取數(shù)據(jù)
std::string dbData = fetchDataFromDB("Data");
//從文件獲取數(shù)據(jù)
std::string fileData = fetchDataFromFile("Data");
//獲取結(jié)束時(shí)間
auto end = system_clock::now();
auto diff = duration_cast<std::chrono::seconds>(end - start).count();
std::cout << "Total Time taken= " << diff << "Seconds" << std::endl;
//組裝數(shù)據(jù)
std::string data = dbData + " :: " + fileData;
//輸出組裝的數(shù)據(jù)
std::cout << "Data = " << data << std::endl;
return 0;
}
輸出:
Total Time Taken = 10 Seconds
Data = DB_Data :: File_Data
由于函數(shù) fetchDataFromDB() 和 fetchDataFromFile()各自在單獨(dú)的線程中運(yùn)行5秒,所以,總共耗時(shí)10秒。
既然從數(shù)據(jù)庫和文件系統(tǒng)中獲取數(shù)據(jù)是獨(dú)立的并且都要耗時(shí),那我們可以并行地運(yùn)行他們。
一種方式是創(chuàng)建一個(gè)新的線程傳遞一個(gè)promise作為線程函數(shù)的參數(shù),并在調(diào)用線程中從關(guān)聯(lián)的std::future對象獲取數(shù)據(jù)
另一種方式就是使用std::async
使用函數(shù)指針調(diào)用std::async作為回調(diào)
修改上面的代碼,并使用std::async異步調(diào)用fetchDataFromDB()
std::future<std::string>resultFromDB = std::async(std::launch::async, fetchDataFromDB, "Data"); std::string dbData = resultDromDB.get()
std::async()做如下的事情
·自動(dòng)創(chuàng)建一個(gè)線程(或從內(nèi)部線程池中挑選)和一個(gè)promise對象。
·然后將std::promise對象傳遞給線程函數(shù),并返回相關(guān)的std::future對象
·當(dāng)我們傳遞參數(shù)的函數(shù)退出時(shí),它的值將被設(shè)置在這個(gè)promise對象中,所以最終的返回值將在std::future對象中可用
現(xiàn)在改變上面的例子,使用std::async異步地從數(shù)據(jù)庫中獲取數(shù)據(jù)
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <future>
using namespace std::chrono;
std::string fetchDataFromDB(std::string recvData) {
//確保函數(shù)要5秒才能執(zhí)行完成
std::this_thread::sleep_for(seconds(5));
//處理創(chuàng)建數(shù)據(jù)庫連接、獲取數(shù)據(jù)等事情
return "DB_" + recvData;
}
std::string fetchDataFromFile(std::string recvData) {
//確保函數(shù)要5秒才能執(zhí)行完成
std::this_thread::sleep_for(seconds(5));
//處理獲取文件數(shù)據(jù)
return "File_" + recvData;
}
int main() {
//獲取開始時(shí)間
system_clock::time_point start = system_clock::now();
std::future<std::string> resultFromDB = std::async(std::launch::async, fetchDataFromDB, "Data");
//從文件獲取數(shù)據(jù)
std::string fileData = fetchDataFromFile("Data");
//從DB獲取數(shù)據(jù)
//數(shù)據(jù)在future<std::string>對象中可獲取之前,將一直阻塞
std::string dbData = resultFromDB.get();
//獲取結(jié)束時(shí)間
auto end = system_clock::now();
auto diff = duration_cast<std::chrono::seconds>(end - start).count();
std::cout << "Total Time taken= " << diff << "Seconds" << std::endl;
//組裝數(shù)據(jù)
std::string data = dbData + " :: " + fileData;
//輸出組裝的數(shù)據(jù)
std::cout << "Data = " << data << std::endl;
return 0;
}
輸出:
Total Time taken= 5Seconds
Data = DB_Data :: File_Data
只使用了5秒
用Function對象作為回調(diào)調(diào)用std::async
/*
* Function Object
*/
struct DataFetcher {
std::string operator ()(std::string recvdData) {
//確保函數(shù)要5秒才能執(zhí)行完成
std::this_thread::sleep_for(seconds(5));
//處理獲取文件數(shù)據(jù)
return "File_" + recvdData;
}
};
//用函數(shù)對象調(diào)用std::async
std::future<std::string> fileResult = std::async(DataFetcher(), "Data");
用lambda函數(shù)作為回調(diào)調(diào)用std::async
std::future<std::string> resultFromDB = std::async([](std::string recvdData) {
std::this_thread::sleep_for(seconds(5));
//處理創(chuàng)建數(shù)據(jù)庫連接、獲取數(shù)據(jù)等事情
return "DB_" + recvdData;
}, "Data");
總結(jié)
到此這篇關(guān)于c++11多線程編程之std::async的文章就介紹到這了,更多相關(guān)c++11多線程編程std::async內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言使用strcmp()函數(shù)比較兩個(gè)字符串的實(shí)現(xiàn)
這篇文章主要介紹了C語言使用strcmp()函數(shù)比較兩個(gè)字符串的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
C++中的頭文件與Extern(外部函數(shù)調(diào)用)方式
這篇文章主要介紹了C++中的頭文件與Extern(外部函數(shù)調(diào)用)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

