C++11中std::packaged_task的使用詳解
C++11中的std::packaged_task是個(gè)模板類(lèi)。std::packaged_task包裝任何可調(diào)用目標(biāo)(函數(shù)、lambda表達(dá)式、bind表達(dá)式、函數(shù)對(duì)象)以便它可以被異步調(diào)用。它的返回值或拋出的異常被存儲(chǔ)于能通過(guò)std::future對(duì)象訪問(wèn)的共享狀態(tài)中。
std::packaged_task類(lèi)似于std::function,但是會(huì)自動(dòng)將其結(jié)果傳遞給std::future對(duì)象。
std::packaged_task對(duì)象內(nèi)部包含兩個(gè)元素:(1).存儲(chǔ)的任務(wù)(stored task)是一些可調(diào)用的對(duì)象(例如函數(shù)指針、成員或函數(shù)對(duì)象的指針)( A stored task, which is some callable object (such as a function pointer, pointer to member or function object))。(2).共享狀態(tài),它可以存儲(chǔ)調(diào)用存儲(chǔ)的任務(wù)(stored task)的結(jié)果,并可以通過(guò)std::future進(jìn)行異步訪問(wèn)(A shared state, which is able to store the results of calling the stored task and be accessed asynchronously through a future)。
通過(guò)調(diào)用std::packaged_task的get_future成員將共享狀態(tài)與std::future對(duì)象關(guān)聯(lián)。調(diào)用之后,兩個(gè)對(duì)象共享相同的共享狀態(tài):(1).std::packaged_task對(duì)象是異步提供程序(asynchronous provider),應(yīng)通過(guò)調(diào)用存儲(chǔ)的任務(wù)(stored task)在某個(gè)時(shí)刻將共享狀態(tài)設(shè)置為就緒。(2).std::future對(duì)象是一個(gè)異步返回對(duì)象,可以檢索共享狀態(tài)的值,并在必要時(shí)等待其準(zhǔn)備就緒。
共享狀態(tài)的生存期至少要持續(xù)到與之關(guān)聯(lián)的最后一個(gè)對(duì)象釋放或銷(xiāo)毀為止。
std::packaged_task不會(huì)自己?jiǎn)?dòng),你必須調(diào)用它(A packaged_task won't start on it's own, you have to invoke it)。
std::future介紹參考:http://www.dbjr.com.cn/article/179229.htm
模板類(lèi)std::packaged_task成員函數(shù)包括:
1. 構(gòu)造函數(shù):(1).默認(rèn)構(gòu)造函數(shù):無(wú)共享狀態(tài)無(wú)存儲(chǔ)任務(wù)(no shared state and no stored task)情況下初始化對(duì)象。(2). initialization constructor:該對(duì)象具有共享狀態(tài),且其存儲(chǔ)的任務(wù)由fn初始化。(3). initialization constructor with allocator。(4).禁用拷貝構(gòu)造。(5).支持移動(dòng)構(gòu)造。
2. 析構(gòu)函數(shù):(1).放棄(abandon)共享狀態(tài)并銷(xiāo)毀packaged_task對(duì)象。(2). 如果有其它future對(duì)象關(guān)聯(lián)到同一共享狀態(tài),則共享狀態(tài)本身不會(huì)被銷(xiāo)毀。(3). 如果packaged_task對(duì)象在共享狀態(tài)準(zhǔn)備就緒前被銷(xiāo)毀,則共享狀態(tài)自動(dòng)準(zhǔn)備就緒并包含一個(gè)std::future_error類(lèi)型的異常。
3. get_future函數(shù):(1).返回一個(gè)與packaged_task對(duì)象的共享狀態(tài)關(guān)聯(lián)的std::future對(duì)象。(2).一旦存儲(chǔ)的任務(wù)被調(diào)用,返回的std::future對(duì)象就可以訪問(wèn)packaged_task對(duì)象在共享狀態(tài)上設(shè)置的值或異常。(3).每個(gè)packaged_task共享狀態(tài)只能被一個(gè)std::future對(duì)象檢索(Only one future object can be retrieved for each packaged_task shared state)。(4).調(diào)用此函數(shù)后,packaged_task應(yīng)在某個(gè)時(shí)候使其共享狀態(tài)準(zhǔn)備就緒(通過(guò)調(diào)用其存儲(chǔ)的任務(wù)),否則將在銷(xiāo)毀后自動(dòng)準(zhǔn)備就緒并包含一個(gè)std::future_error類(lèi)型的異常。
4. make_ready_at_thread_exit函數(shù):在線程退出時(shí)才使共享狀態(tài)ready而不是在調(diào)用完成后就立即ready。
5. operator=:(1).禁用拷貝賦值。(2).支持移動(dòng)賦值。
6. operator():(1).call stored task。(2).如果對(duì)存儲(chǔ)任務(wù)的調(diào)用成功完成或拋出異常,則返回的值或捕獲的異常存儲(chǔ)在共享狀態(tài),共享狀態(tài)準(zhǔn)備就緒(解除阻塞當(dāng)前等待它的所有線程)。
7. reset函數(shù):(1).在保持相同存儲(chǔ)的任務(wù)的同時(shí),以新的共享狀態(tài)重置對(duì)象。(2).允許再次調(diào)用存儲(chǔ)的任務(wù)。(3).與對(duì)象關(guān)聯(lián)的之前的共享狀態(tài)被放棄(就像packaged_task被銷(xiāo)毀了一樣)。(4).在內(nèi)部,該函數(shù)的行為就像是移動(dòng)賦值了一個(gè)新構(gòu)造的packaged_task一樣(Internally, the function behaves as if move-assigned a newly constructed packaged_task (with its stored task as argument))。
8. swap函數(shù)/非成員模板函數(shù)swap:交換共享狀態(tài)和存儲(chǔ)的任務(wù)(stored task)。
9. valid函數(shù):檢查packaged_task對(duì)象是否具有共享狀態(tài)。
詳細(xì)用法見(jiàn)下面的測(cè)試代碼,下面是從其他文章中copy的測(cè)試代碼,部分作了調(diào)整,詳細(xì)內(nèi)容介紹可以參考對(duì)應(yīng)的reference:
#include "future.hpp"
#include <iostream>
#include <future>
#include <chrono>
#include <utility>
#include <thread>
#include <functional>
#include <memory>
#include <exception>
#include <numeric>
#include <vector>
#include <cmath>
#include <string>
namespace future_ {
///////////////////////////////////////////////////////////
// reference: http://www.cplusplus.com/reference/future/packaged_task/
int test_packaged_task_1()
{
{ // constructor/get_future/operator=/valid
std::packaged_task<int(int)> foo; // default-constructed
std::packaged_task<int(int)> bar([](int x) { return x * 2; }); // initialized
foo = std::move(bar); // move-assignment
std::cout << "valid: " << foo.valid() << "\n";
std::future<int> ret = foo.get_future(); // get future
std::thread(std::move(foo), 10).detach(); // spawn thread and call task
int value = ret.get(); // wait for the task to finish and get result
std::cout << "The double of 10 is " << value << ".\n";
}
{ // reset/operator()
std::packaged_task<int(int)> tsk([](int x) { return x * 3; }); // package task
std::future<int> fut = tsk.get_future();
tsk(33);
std::cout << "The triple of 33 is " << fut.get() << ".\n";
// re-use same task object:
tsk.reset();
fut = tsk.get_future();
std::thread(std::move(tsk), 99).detach();
std::cout << "Thre triple of 99 is " << fut.get() << ".\n";
}
{ // constructor/get_future
auto countdown = [](int from, int to) {
for (int i = from; i != to; --i) {
std::cout << i << '\n';
std::this_thread::sleep_for(std::chrono::seconds(1));
}
std::cout << "Lift off!\n";
return from - to;
};
std::packaged_task<int(int, int)> tsk(countdown); // set up packaged_task
std::future<int> ret = tsk.get_future(); // get future
std::thread th(std::move(tsk), 5, 0); // spawn thread to count down from 5 to 0
int value = ret.get(); // wait for the task to finish and get result
std::cout << "The countdown lasted for " << value << " seconds.\n";
th.join();
}
return 0;
}
///////////////////////////////////////////////////////////
// reference: https://en.cppreference.com/w/cpp/thread/packaged_task
int test_packaged_task_2()
{
{ // lambda
std::packaged_task<int(int, int)> task([](int a, int b) { return std::pow(a, b);});
std::future<int> result = task.get_future();
task(2, 9);
std::cout << "task_lambda:\t" << result.get() << '\n';
}
{ // bind
std::packaged_task<int()> task(std::bind([](int x, int y) { return std::pow(x, y); }, 2, 11));
std::future<int> result = task.get_future();
task();
std::cout << "task_bind:\t" << result.get() << '\n';
}
{ // thread
std::packaged_task<int(int, int)> task([](int x, int y) { return std::pow(x, y); });
std::future<int> result = task.get_future();
std::thread task_td(std::move(task), 2, 10);
task_td.join();
std::cout << "task_thread:\t" << result.get() << '\n';
}
return 0;
}
///////////////////////////////////////////////////////////
// reference: https://thispointer.com/c11-multithreading-part-10-packaged_task-example-and-tutorial/
struct DBDataFetcher {
std::string operator()(std::string token)
{
// Do some stuff to fetch the data
std::string data = "Data From " + token;
return data;
}
};
int test_packaged_task_3()
{
// Create a packaged_task<> that encapsulated a Function Object
std::packaged_task<std::string(std::string)> task(std::move(DBDataFetcher()));
// Fetch the associated future<> from packaged_task<>
std::future<std::string> result = task.get_future();
// Pass the packaged_task to thread to run asynchronously
std::thread th(std::move(task), "Arg");
// Join the thread. Its blocking and returns when thread is finished.
th.join();
// Fetch the result of packaged_task<> i.e. value returned by getDataFromDB()
std::string data = result.get();
std::cout << data << std::endl;
return 0;
}
///////////////////////////////////////////////////////////
// reference: https://stackoverflow.com/questions/18143661/what-is-the-difference-between-packaged-task-and-async
int test_packaged_task_4()
{
// sleeps for one second and returns 1
auto sleep = []() {
std::this_thread::sleep_for(std::chrono::seconds(1));
return 1;
};
{ // std::packaged_task
// >>>>> A packaged_task won't start on it's own, you have to invoke it
std::packaged_task<int()> task(sleep);
auto f = task.get_future();
task(); // invoke the function
// You have to wait until task returns. Since task calls sleep
// you will have to wait at least 1 second.
std::cout << "You can see this after 1 second\n";
// However, f.get() will be available, since task has already finished.
std::cout << f.get() << std::endl;
}
{ // std::async
// >>>>> On the other hand, std::async with launch::async will try to run the task in a different thread :
auto f = std::async(std::launch::async, sleep);
std::cout << "You can see this immediately!\n";
// However, the value of the future will be available after sleep has finished
// so f.get() can block up to 1 second.
std::cout << f.get() << "This will be shown after a second!\n";
}
return 0;
}
} // namespace future_
GitHub:https://github.com/fengbingchun/Messy_Test
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解c++11以正確的姿勢(shì)輸出enum class的值
這篇文章主要介紹了詳解c++11以正確的姿勢(shì)輸出enum class的值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
淺談C++對(duì)象的內(nèi)存分布和虛函數(shù)表
下面小編就為大家?guī)?lái)一篇淺談C++對(duì)象的內(nèi)存分布和虛函數(shù)表。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
C語(yǔ)言中關(guān)于計(jì)算字符串長(zhǎng)度的幾種方式
這篇文章主要介紹了C語(yǔ)言中關(guān)于計(jì)算字符串長(zhǎng)度的幾種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
C++常用的11種設(shè)計(jì)模式解釋及示例代碼詳解
c++常用的設(shè)計(jì)模式包括單例模式、工廠模式、抽象工廠模式、適配器模式、裝飾者模式、代理模式、外觀模式、橋接模式、組合模式、享元模式、觀察者模式和命令模式等,這篇文章主要介紹了C++常用的11種設(shè)計(jì)模式解釋及示例,需要的朋友可以參考下2023-02-02
C++ 自由存儲(chǔ)區(qū)是否等價(jià)于堆你知道嗎
自由存儲(chǔ)是C++中通過(guò)new與delete動(dòng)態(tài)分配和釋放對(duì)象的抽象概念,而堆(heap)是C語(yǔ)言和操作系統(tǒng)的術(shù)語(yǔ),是操作系統(tǒng)維護(hù)的一塊動(dòng)態(tài)分配內(nèi)存2021-08-08
C++ float、double判斷是否等于0問(wèn)題
這篇文章主要介紹了C++ float、double判斷是否等于0問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

