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

什么是 std::promise?

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

問題描述

我對 C++11 的 std::threadstd::asyncstd::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 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.

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

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

相關(guān)文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉(zhuǎn)置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉(zhuǎn)一個點 (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++ 中構(gòu)建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 亚洲国产高清高潮精品美女 | 欧美极品一区二区 | 亚洲精品自在在线观看 | 美女131mm久久爽爽免费 | 日韩精品一二三 | 成人在线电影网站 | 天堂久久天堂综合色 | 午夜天堂精品久久久久 | 亚洲成人一区二区 | 浮生影院免费观看中文版 | 久夜精品| 精品美女 | 亚洲日本一区二区 | 99日韩 | 91资源在线观看 | 日韩在线欧美 | 亚洲欧美日韩中文在线 | 岛国精品| jizz在线看片 | 亚洲精品电影网在线观看 | 99精品网站| 91免费电影 | 国产在线中文字幕 | 精品av| 亚洲在线免费 | 国产激情视频 | 日韩色综合 | 亚洲天天干 | 国产乱码精品一区二区三区中文 | 久久久久久久久久久91 | 日韩成人在线视频 | 国产亚洲www| 日韩欧美一级精品久久 | 一级黄色裸片 | 日韩a视频 | 免费一级做a爰片久久毛片潮喷 | 国产欧美一区二区在线观看 | 午夜av成人 | 九九视频在线观看视频6 | 欧美中文字幕在线观看 | 野狼在线社区2017入口 |