Use std::shared_future<T> when several consumers need the same one-off asynchronous result. Unlike the move-only std::future<T>, a shared future is copyable: each copy refers to the same associated state, becomes ready at the same time, and can access its result. Use std::shared_future<void> when the event carries no value. section 4.2 section 4.2.5
Turn a unique future into a shared future
A promise's get_future() supplies a std::future<int>. Moving that future into std::shared_future<int> transfers the reference to the result: the original future becomes invalid, while the shared future is valid. Alternatively, call share() on a future; each consumer can then receive a copy. section 4.2.5
std::promise<int> p; std::future<int> f(p.get_future()); assert(f.valid()); #1 std::shared_future<int> sf(std::move(f)); assert(!f.valid()); #2 assert(sf.valid()); #3
The validity checks make the ownership transfer visible. section 4.2.5
Copy the shared future for each thread rather than accessing one shared_future object concurrently. Access to an individual future object is not synchronized; unprotected concurrent access can cause a data race. Separate copies safely access their common asynchronous state through library synchronization. section 4.2.5
One result, several dependencies
In a parallel spreadsheet, one cell's final value may feed several formulas. Each dependent task can hold its own shared_future for that value and wait if the cell is not ready, while unrelated tasks proceed. section 4.2.5
The separate Concurrency TS type std::experimental::shared_future also supports multiple continuations on one result. Each continuation receives an experimental shared future; its return value is an experimental future unless shared again. section 4.4.4
auto fut=spawn_async(some_function).share();
auto fut2=fut.then([](std::experimental::shared_future<some_data> data){
do_stuff(data);
});
auto fut3=fut.then([](std::experimental::shared_future<some_data> data){
return do_other_stuff(data);
});
Two continuations attach to the same experimental shared future. section 4.4.4
