来自 http://www.cplusplus.com/reference/future/promise/get_future/ :
After this function has been called, the promise is expected to make its shared state ready at some point [...]
我不确定这是否意味着此操作顺序是强制性的:
- get_future()
- 设置值()
是否也有可能只有在设定值后才能从 promise 中获得 future ?
最佳答案
据我所知没有这样的限制。 std::promise::set_value
导致错误的仅有两种情况是:
Promise 对象没有共享状态(这可能在移动 promise 对象时发生):
promise<int> p; auto p2 = std::move(p); p.set_value(42); // error
共享状态已经存储了一个值或异常:
promise<int> p; p.set_value(0); p.set_value(42); // error
或
promise<int> p; try { throw std::runtime_error("Some error"); } catch(...) { p.set_exception(std::current_exception()); p.set_value(42); // error }
但是get_future
之前调用没有限制
关于c++ - 在设置值之前是否必须调用 promise.get_future() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56147196/