shared_future::get
and deferred async
functionsSection: 32.10.8 [futures.shared.future] Status: Resolved Submitter: Anthony Williams Opened: 2009-11-17 Last modified: 2021-06-06
Priority: Not Prioritized
View all other issues in [futures.shared.future].
View all issues with Resolved status.
Discussion:
If a shared_future
is constructed with the result of an async
call with a
deferred function, and two or more copies of that shared_future
are created,
with multiple threads calling get()
, it is not clear which thread runs the
deferred function. [futures.shared_future]p22 from
N3000
says (minus editor's note):
Effects: if the associated state contains a deferred function, executes the deferred function. Otherwise, blocks until the associated state is ready.
In the absence of wording to the contrary, this implies that every thread that
calls wait()
will execute the deferred function.
[
2010 Pittsburgh: Moved to NAD EditorialResolved. Rationale added below.
]
Rationale:
Solved by N3058.
Proposed resolution:
Replace [futures.shared_future]p22 with the following:
Effects: If the associated state
contains a deferred function, executes the deferred function. Otherwise, blocks until the associated state is ready.was created by apromise
orpackaged_task
object, block until the associated state is ready. If the associated state is associated with a thread created for anasync
call (32.10.9 [futures.async]), as ifassociated-thread.join()
.If the associated state contains a deferred function, calls to
wait()
on allshared_future
objects that share the same associated state are serialized. The first call towait()
that shares a given associated state executes the deferred function and stores the return value or exception in the associated state.Synchronization: if the associated state was created by a
promise
object, the completion ofset_value()
orset_exception()
to thatpromise
happens before (6.9.2 [intro.multithread])wait()
returns. If the associated state was created by apackaged_task
object, the completion of the associated task happens beforewait()
returns. If the associated state is associated with a thread created for anasync
call (32.10.9 [futures.async]), the completion of the associated thread happens-beforewait()
returns.If the associated state contained a deferred function, the invocation of the deferred function happens-before any call to
wait()
on afuture
that shares that state returns.