欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c++11多線程編程之std::async的介紹與實(shí)例

 更新時(shí)間:2020年11月02日 10:51:37   作者:小麒麟666  
這篇文章主要給大家介紹了關(guān)于c++11多線程編程之std::async的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

本節(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ú)的線程中執(zhí)行

·std::launch::deferred

當(dāng)其他線程調(diào)用get()來(lái)訪問(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ù)(字符串),然后需要合并字符串并打印。

在單線程中,我們這樣做:

#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ú)的線程中運(yùn)行5秒,所以,總共耗時(shí)10秒。

既然從數(shù)據(jù)庫(kù)和文件系統(tǒng)中獲取數(shù)據(jù)是獨(dú)立的并且都要耗時(shí),那我們可以并行地運(yùn)行他們。

一種方式是創(chuàng)建一個(gè)新的線程傳遞一個(gè)promise作為線程函數(shù)的參數(shù),并在調(diào)用線程中從關(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è)線程(或從內(nèi)部線程池中挑選)和一個(gè)promise對(duì)象。

·然后將std::promise對(duì)象傳遞給線程函數(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多線程編程之std::async的文章就介紹到這了,更多相關(guān)c++11多線程編程std::async內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于C語(yǔ)言航班信息查詢與檢索

    基于C語(yǔ)言航班信息查詢與檢索

    這篇文章主要為大家詳細(xì)介紹了基于C語(yǔ)言航班信息查詢與檢索,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 淺談QT內(nèi)存泄漏

    淺談QT內(nèi)存泄漏

    本文主要介紹了淺談QT內(nèi)存泄漏,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • C語(yǔ)言 指針與二維數(shù)組詳解

    C語(yǔ)言 指針與二維數(shù)組詳解

    本文主要介紹C語(yǔ)言 指針與二維數(shù)組,這里整理了詳細(xì)的資料及示例代碼,有需要的小伙伴可以參考下
    2016-08-08
  • C語(yǔ)言使用strcmp()函數(shù)比較兩個(gè)字符串的實(shí)現(xiàn)

    C語(yǔ)言使用strcmp()函數(shù)比較兩個(gè)字符串的實(shí)現(xiàn)

    這篇文章主要介紹了C語(yǔ)言使用strcmp()函數(shù)比較兩個(gè)字符串的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 利用Matlab制作一款刮刮樂(lè)抽獎(jiǎng)特效

    利用Matlab制作一款刮刮樂(lè)抽獎(jiǎng)特效

    七夕節(jié)還不知道送啥,教你用MATLAB制作一款刮刮樂(lè)抽獎(jiǎng)特效,讓她的手氣決定她的禮物。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-03-03
  • 利用Matlab制作一款狗頭翻牌子小游戲

    利用Matlab制作一款狗頭翻牌子小游戲

    本文將用Matlab制作一個(gè)狗頭翻牌子的小游戲,就是點(diǎn)擊一個(gè)牌子時(shí),該牌子和周?chē)膫€(gè)牌子也會(huì)相應(yīng)發(fā)生變化,想辦法讓所有牌子都在同一面即為游戲勝利。感興趣的可以跟隨小編學(xué)習(xí)一下
    2022-03-03
  • C++內(nèi)聯(lián)函數(shù)詳情

    C++內(nèi)聯(lián)函數(shù)詳情

    這篇文章主要介紹了C++內(nèi)聯(lián)函數(shù),文章主要圍繞C++內(nèi)聯(lián)函數(shù)的相關(guān)資料展開(kāi)詳細(xì)內(nèi)容,需要的朋友可以參考一下,希望對(duì)大家有所幫助
    2021-11-11
  • C語(yǔ)言進(jìn)度條的實(shí)現(xiàn)原理詳解

    C語(yǔ)言進(jìn)度條的實(shí)現(xiàn)原理詳解

    這篇文章主要介紹了C語(yǔ)言進(jìn)度條的實(shí)現(xiàn)原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • C++中的頭文件與Extern(外部函數(shù)調(diào)用)方式

    C++中的頭文件與Extern(外部函數(shù)調(diào)用)方式

    這篇文章主要介紹了C++中的頭文件與Extern(外部函數(shù)調(diào)用)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • C++ Boost Exception超詳細(xì)講解

    C++ Boost Exception超詳細(xì)講解

    Boost是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱。Boost庫(kù)是一個(gè)可移植、提供源代碼的C++庫(kù),作為標(biāo)準(zhǔn)庫(kù)的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開(kāi)發(fā)引擎之一,是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱
    2022-11-11

最新評(píng)論