問題描述
我對 C++11 的 std::thread
、std::async
和 std::future
組件相當(dāng)熟悉(例如,請參閱此答案),這些很簡單.
I'm fairly familiar with C++11's std::thread
, std::async
and std::future
components (e.g. see this answer), which are straight-forward.
然而,我無法完全理解 std::promise
是什么,它的作用是什么,在什么情況下最適合使用.標(biāo)準(zhǔn)文檔本身不包含除類概要之外的大量信息,std::thread.
However, I cannot quite grasp what std::promise
is, what it does and in which situations it is best used. The standard document itself doesn't contain a whole lot of information beyond its class synopsis, and neither does std::thread.
有人可以舉一個簡短的例子來說明需要 std::promise
的情況以及它是最慣用的解決方案嗎?
Could someone please give a brief, succinct example of a situation where an std::promise
is needed and where it is the most idiomatic solution?
推薦答案
用[futures.state]的話來說,std::future
是一個異步返回對象(從共享狀態(tài)讀取結(jié)果的對象"),std::promise
是異步提供者(向共享狀態(tài)提供結(jié)果的對象") 即承諾是您設(shè)置結(jié)果的東西,以便您可以從相關(guān)的未來獲得它.
In the words of [futures.state] a std::future
is an asynchronous return object ("an object that reads results from a shared state") and a std::promise
is an asynchronous provider ("an object that provides a result to a shared state") i.e. a promise is the thing that you set a result on, so that you can get it from the associated future.
異步提供者最初創(chuàng)建未來引用的共享狀態(tài).std::promise
是一種異步提供者,std::packaged_task
是另一種,std::async
的內(nèi)部細節(jié)是另一種.每個都可以創(chuàng)建一個共享狀態(tài),并為您提供一個共享該狀態(tài)的 std::future
,并且可以使該狀態(tài)準(zhǔn)備就緒.
The asynchronous provider is what initially creates the shared state that a future refers to. std::promise
is one type of asynchronous provider, std::packaged_task
is another, and the internal detail of std::async
is another. Each of those can create a shared state and give you a std::future
that shares that state, and can make the state ready.
std::async
是一個更高級別的便利實用程序,它為您提供異步結(jié)果對象,并在內(nèi)部負責(zé)創(chuàng)建異步提供程序并在任務(wù)完成時準(zhǔn)備共享狀態(tài).你可以用一個 std::packaged_task
(或 std::bind
和一個 std::promise
)和一個 std 來模擬它::thread
但使用 std::async
更安全、更容易.
std::async
is a higher-level convenience utility that gives you an asynchronous result object and internally takes care of creating the asynchronous provider and making the shared state ready when the task completes. You could emulate it with a std::packaged_task
(or std::bind
and a std::promise
) and a std::thread
but it's safer and easier to use std::async
.
std::promise
有點低級,用于當(dāng)你想將異步結(jié)果傳遞給未來,但使結(jié)果就緒的代碼不能包含在單個函數(shù)中適合傳遞給 std::async
.例如,您可能有一個由多個 promise
和關(guān)聯(lián)的 future
組成的數(shù)組,并且有一個線程來執(zhí)行多個計算并為每個 promise 設(shè)置一個結(jié)果.async
只允許你返回一個結(jié)果,返回多個你需要多次調(diào)用 async
,這可能會浪費資源.
std::promise
is a bit lower-level, for when you want to pass an asynchronous result to the future, but the code that makes the result ready cannot be wrapped up in a single function suitable for passing to std::async
. For example, you might have an array of several promise
s and associated future
s and have a single thread which does several calculations and sets a result on each promise. async
would only allow you to return a single result, to return several you would need to call async
several times, which might waste resources.
這篇關(guān)于什么是 std::promise?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!