c++11多線(xiàn)程編程之std::async的介紹與實(shí)例
本節(jié)討論下在C++11中怎樣使用std::async來(lái)執(zhí)行異步task。
C++11中引入了std::async
什么是std::async
std::async()是一個(gè)接受回調(diào)(函數(shù)或函數(shù)對(duì)象)作為參數(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ù)對(duì)象返回的值。
函數(shù)期望的參數(shù)可以作為函數(shù)指針參數(shù)后面的參數(shù)傳遞給std::async()。
std::async中的第一個(gè)參數(shù)是啟動(dòng)策略,它控制std::async的異步行為,我們可以用三種不同的啟動(dòng)策略來(lái)創(chuàng)建std::async
·std::launch::async
保證異步行為,即傳遞函數(shù)將在單獨(dú)的線(xiàn)程中執(zhí)行
·std::launch::deferred
當(dāng)其他線(xiàn)程調(diào)用get()來(lái)訪(fǎng)問(wèn)共享狀態(tài)時(shí),將調(diào)用非異步行為
·std::launch::async | std::launch::deferred
默認(rèn)行為。有了這個(gè)啟動(dòng)策略,它可以異步運(yùn)行或不運(yùn)行,這取決于系統(tǒng)的負(fù)載,但我們無(wú)法控制它。
如果我們不指定一個(gè)啟動(dòng)策略,其行為將類(lèi)似于std::launch::async | std::launch::deferred
本節(jié)我們將使用std::launch::async啟動(dòng)策略
我們可以在std::async傳遞任何回調(diào),如:
·函數(shù)指針
·函數(shù)對(duì)象
·lambda表達(dá)式
std::async的需求
假設(shè)我們必須從數(shù)據(jù)庫(kù)和文件系統(tǒng)里里獲取一些數(shù)據(jù)(字符串),然后需要合并字符串并打印。
在單線(xiàn)程中,我們這樣做:
#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ù)庫(kù)連接、獲取數(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() {
//獲取開(kāi)始時(shí)間
system_clock::time_point start = system_clock::now();
//從數(shù)據(jù)庫(kù)獲取數(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ú)的線(xiàn)程中運(yùn)行5秒,所以,總共耗時(shí)10秒。
既然從數(shù)據(jù)庫(kù)和文件系統(tǒng)中獲取數(shù)據(jù)是獨(dú)立的并且都要耗時(shí),那我們可以并行地運(yùn)行他們。
一種方式是創(chuàng)建一個(gè)新的線(xiàn)程傳遞一個(gè)promise作為線(xiàn)程函數(shù)的參數(shù),并在調(diào)用線(xiàn)程中從關(guān)聯(lián)的std::future對(duì)象獲取數(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è)線(xiàn)程(或從內(nèi)部線(xiàn)程池中挑選)和一個(gè)promise對(duì)象。
·然后將std::promise對(duì)象傳遞給線(xiàn)程函數(shù),并返回相關(guān)的std::future對(duì)象
·當(dāng)我們傳遞參數(shù)的函數(shù)退出時(shí),它的值將被設(shè)置在這個(gè)promise對(duì)象中,所以最終的返回值將在std::future對(duì)象中可用
現(xiàn)在改變上面的例子,使用std::async異步地從數(shù)據(jù)庫(kù)中獲取數(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ù)庫(kù)連接、獲取數(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() {
//獲取開(kāi)始時(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>對(duì)象中可獲取之前,將一直阻塞
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對(duì)象作為回調(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ù)對(duì)象調(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ù)庫(kù)連接、獲取數(shù)據(jù)等事情
return "DB_" + recvdData;
}, "Data");
總結(jié)
到此這篇關(guān)于c++11多線(xiàn)程編程之std::async的文章就介紹到這了,更多相關(guān)c++11多線(xiàn)程編程std::async內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Eclipse中C++連接mysql數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了Eclipse中C++連接mysql數(shù)據(jù)庫(kù) ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
C語(yǔ)言基礎(chǔ)之二分查找知識(shí)最全匯總
這篇文章主要介紹了C語(yǔ)言基礎(chǔ)之二分查找知識(shí)最全匯總,文中有非常詳細(xì)的二分查找基礎(chǔ)知識(shí)詳解,對(duì)正在學(xué)習(xí)C語(yǔ)言基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
C 語(yǔ)言中實(shí)現(xiàn)環(huán)形緩沖區(qū)
本文主要是介紹 C語(yǔ)言實(shí)現(xiàn)環(huán)形緩沖區(qū),并附有詳細(xì)實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,希望能幫助有需要的小伙伴2016-07-07
C語(yǔ)言遞歸在實(shí)踐題目中應(yīng)用詳解
遞歸是C語(yǔ)言中非常重要的知識(shí)點(diǎn),其中的大事化小等思想對(duì)初學(xué)C語(yǔ)言的小伙伴來(lái)說(shuō)不是很友好,因此我整理了遞歸的經(jīng)典題目并向外拓展,給你全面的介紹,重新認(rèn)識(shí)遞歸2022-05-05
C++超詳細(xì)講解內(nèi)存空間分配與this指針
this?指針在C++類(lèi)和對(duì)象中是個(gè)很方便實(shí)用的關(guān)鍵字,可以簡(jiǎn)化對(duì)象成員屬性的調(diào)用,使代碼表達(dá)的含義更加準(zhǔn)確;在之前的學(xué)習(xí)中我們都可以判斷變量所占內(nèi)存空間大小,那么我們創(chuàng)建的類(lèi)對(duì)象所占的內(nèi)存空間怎么計(jì)算呢?想知道this的妙用和類(lèi)對(duì)象占用的內(nèi)存空間就來(lái)跟我學(xué)習(xí)吧2022-05-05
QT自定義QTextEdit實(shí)現(xiàn)大數(shù)據(jù)的實(shí)時(shí)刷新顯示功能實(shí)例
TextEdit是我們常用的Qt控件,用來(lái)顯示文本信息,下面這篇文章主要給大家介紹了關(guān)于QT自定義QTextEdit實(shí)現(xiàn)大數(shù)據(jù)的實(shí)時(shí)刷新顯示功能的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
QT的QWebEngineView類(lèi)知識(shí)點(diǎn)詳細(xì)介紹
QWebEngineView是Qt框架中的組件,基于Chromium內(nèi)核,支持HTML5、CSS3、JavaScript等Web技術(shù),適用于嵌入網(wǎng)頁(yè)內(nèi)容到Qt應(yīng)用程序,它提供了豐富的接口如加載、導(dǎo)航、與JavaScript交互等,并支持信號(hào)槽機(jī)制處理各種網(wǎng)頁(yè)事件,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10

