Is it legal to call shared_future::get() multiple times on the same instance in the same thread?

Per cppreference in std::shared_future<T>::valid

Unlike std::future, std::shared_future's shared state is not invalidated when get() is called.

Which makes sense. If it wasn't the case then you couldn't have multiple threads be able to call 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

releases any shared state ([futures.state]).

while in [futures.shared.future]/18 it states no such thing so the state is still valid after get is called.


boost::shared_future has the same behavior. 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.


AFAIK this is legal. std::shared_future<T>::get() says:

The behavior is undefined if valid() is false before the call to this function.

Going to std::shared_future<T>::valid() it says:

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.

Which would make multiple get() calls from the same thread and on the same instance valid.

Tags:

C++

Boost

Future