[英]Is it legal to call shared_future::get() multiple times on the same instance in the same thread?
I can find neither direct confirmation nor refutation on the matter. 我既不能直接确认也不能驳斥此事。 All answers seem to address the "access from multiple threads" aspect, rather than repetitive access itself.
所有答案似乎都解决了“从多个线程访问”的问题,而不是重复访问本身。
Does the standard define the behavior for std::shared_future
? 标准是否定义了
std::shared_future
的行为? What about boost::shared_future
? 那么
boost::shared_future
呢?
Per cppreference in std::shared_future<T>::valid
在
std::shared_future<T>::valid
每个cppreference
Unlike std::future, std::shared_future's shared state is not invalidated when get() is called.
与std :: future不同,调用get()时,std :: shared_future的共享状态不会失效。
Which makes sense. 这是有道理的。 If it wasn't the case then you couldn't have multiple threads be able to call
get
. 如果不是这种情况那么你就不能让多个线程能够调用
get
。 We can further back this up by looking at the standard. 我们可以通过查看标准来进一步提高这一点。 In [futures.unique.future]/15 they explicitly state
get
only works once with 在[futures.unique.future] / 15 ,他们明确规定
get
只使用一次
releases any shared state ([futures.state]).
释放任何共享状态([futures.state])。
while in [futures.shared.future]/18 it states no such thing so the state is still valid after get
is called. 虽然在[futures.shared.future] / 18中它没有说明这样的事情,所以在调用
get
之后状态仍然有效。
boost::shared_future
has the same behavior. boost::shared_future
具有相同的行为。 Per the reference get
has no text stating it invalidates the shared state on a call to get
so you can call it multiple times. 每参考
get
没有文字说明它无效通话中的共享状态来get
,所以你可以把它多次。
AFAIK this is legal. AFAIK这是合法的。
std::shared_future<T>::get()
says : std::shared_future<T>::get()
说 :
The behavior is undefined if
valid()
isfalse
before the call to this function.如果在调用此函数之前
valid()
为false
,则行为未定义。
Going to std::shared_future<T>::valid()
it says : 转到
std::shared_future<T>::valid()
它说 :
Checks if the future refers to a shared state.
检查未来是否涉及共享状态。
...
...
Unlike
std::future
,std::shared_future
's shared state is not invalidated whenget()
is called .与
std::future
不同, 调用get()
时 ,std::shared_future
的共享状态不会失效 。
Which would make multiple get()
calls from the same thread and on the same instance valid. 这将使来自同一线程和同一实例的多个
get()
调用有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.