什么是std :: promise? [英] What is std::promise?

查看:236
本文介绍了什么是std :: promise?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常熟悉新的标准库的 std :: thread std :: async code> std :: future 组件(例如,请参阅此答案),其中直接。

I'm fairly familiar with the new standard library's std::thread, std::async and std::future components (e.g. see this answer), which are straight-forward.

但是,我不能很好地理解 std :: promise 是什么,哪种情况下最好使用。标准文档本身不包含超出其类概要的大量信息,也不只是:: 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 just::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 是一个异步提供者(提供结果到共享状态的对象),即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 是另一种, code> 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 :: async code>。

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 s和关联的 future s,并且有一个线程做几个计算和设置每个承诺的结果。 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆