This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of Resolved status.
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 apromiseorpackaged_taskobject, block until the associated state is ready. If the associated state is associated with a thread created for anasynccall (32.10.9 [futures.async]), as ifassociated-thread.join().If the associated state contains a deferred function, calls to
wait()on allshared_futureobjects 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
promiseobject, the completion ofset_value()orset_exception()to thatpromisehappens before (6.10.2 [intro.multithread])wait()returns. If the associated state was created by apackaged_taskobject, the completion of the associated task happens beforewait()returns. If the associated state is associated with a thread created for anasynccall (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 afuturethat shares that state returns.