cppreference is explicit about calling std::shared_future<T>::wait
from multiple threads:
Calling wait on the same std::shared_future from multiple threads is not safe; the intended use is for each thread that waits on the same shared state to have a copy of a std::shared_future.
But I can't find a basis for this assertion. There is nothing in the standard marking wait
as some kind of special case. While the standard says that the methods on a single shared_future
instance are not synchronized, you don't need synchronization as long only const methods are being called:
[17.6.5.9/3] A C++ standard library function shall not directly or indirectly modify objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s non-const arguments, including this.
There are contradicting answers to be found on SO. Does anyone have an authoritative source on this which explains how calling a const
method on a from stdlib could lead to a race condition?
shared_future
do not synchronize with themselves, but they synchronize with the shared state" – Collenecolletshared_future
only hasconst
methods. – Vittorioconst
is a guarantee for the standard library, see the quoted passage (17.6.5.9/3).const
operations are expected not to cause race conditions with otherconst
operations there. – Blottershared_future
. – Blotter