久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

什么是 std::promise?

What is std::promise?(什么是 std::promise?)
本文介紹了什么是 std::promise?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我對 C++11 的 std::threadstd::asyncstd::future 組件相當熟悉(例如,請參閱此答案),這些很簡單.

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 是什么,它的作用是什么,在什么情況下最適合使用.標準文檔本身不包含除類概要之外的大量信息,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是一個異步返回對象(從共享狀態讀取結果的對象"),std::promise異步提供者(向共享狀態提供結果的對象") 即承諾是您設置結果的東西,以便您可以從相關的未來獲得它.

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.

異步提供者最初創建未來引用的共享狀態.std::promise 是一種異步提供者,std::packaged_task 是另一種,std::async 的內部細節是另一種.每個都可以創建一個共享狀態,并為您提供一個共享該狀態的 std::future,并且可以使該狀態準備就緒.

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 是一個更高級別的便利實用程序,它為您提供異步結果對象,并在內部負責創建異步提供程序并在任務完成時準備共享狀態.你可以用一個 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 有點低級,用于當你想將異步結果傳遞給未來,但使結果就緒的代碼不能包含在單個函數中適合傳遞給 std::async.例如,您可能有一個由多個 promise 和關聯的 future 組成的數組,并且有一個線程來執行多個計算并為每個 promise 設置一個結果.async 只允許你返回一個結果,返回多個你需要多次調用 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 promises and associated futures 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.

這篇關于什么是 std::promise?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 狠狠干美女 | 午夜av片 | 国产精品久久久久久妇女6080 | 91精品国自产在线观看 | 国产真实乱人偷精品 | 黄视频免费在线观看 | 婷婷综合五月天 | 成人扒开伸进免费观看 | 中文字幕免费在线 | 久艹视频在线观看 | 九九精品在线观看 | 久久av资源 | 天天干天天舔 | 色婷婷一区二区三区四区 | 精品国产区一区二 | 国产激情网站 | 日韩在线播放视频 | 日韩欧美在线观看 | 亚洲午夜一区 | 欧美成人久久 | 国产区一区 | 在线中文字幕视频 | 糖心vlog精品一区二区 | 中文字幕伊人 | 久久xxx | 亚洲砖区区免费 | 国产美女免费 | 国产一区二区不卡视频 | 午夜999| 视频一区在线观看 | 中文字幕久久久 | 男人的天堂亚洲 | 特黄网站 | hdxxxxhd100%| 午夜影院黄 | 天天看天天干 | 男女激情视频网站 | 午夜精品久久久久久久99 | 日韩视频中文字幕 | 欧美日韩激情视频 | 中文字幕不卡 |