简体   繁体   English

在同一个线程中的同一个实例上多次调用shared_future :: get()是否合法?

[英]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() is false 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 when get() 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM