是什么赋予了std :: future一些共享状态 [英] What gives a std::future some shared state
问题描述
std::future
的 get
, wait
, wait_until
要求将来的对象具有共享状态作为前提(如果valid() == false
,它们具有未定义的行为).那么std::future
获得一些共享状态并变得有用吗?
A std::future
has a std::future::valid
method, which indicates whether the future object refers to some shared state. None of the constructors construct a future object that refers to shared state (except the move constructor, which can move the shared state from one future object to another). All the methods of the class (get
, wait
, wait_for
and wait_until
require the future object to have shared state as a precondition (they have undefined behaviour if valid() == false
). How then can a std::future
acquire some shared state and become useful?
推荐答案
通常,您通常不单独使用std::future
.您可以将其与 std::promise
一起使用. std::promise
构造函数设置了一些共享状态.然后,您可以使用 std::promise::get_future()
方法来获取
You do not normally use a std::future
in isolation; you use it with a std::promise
. The std::promise
constructors set up some shared state. You can then use the std::promise::get_future()
method to get a std::future
that refers to that shared state.
这篇关于是什么赋予了std :: future一些共享状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!