A future represents a one-off event whose result may not be available yet. A thread can do other work, check for readiness periodically, or wait when it needs the result. The event may carry a value or simply signal completion; once ready, its future cannot be reset. section 4.2
Return a background result
Start a calculation with `std::future<int> answer = std::async(find_answer);`, do unrelated work, then call `answer.get()`. Unlike `std::thread`, `std::async` supplies a direct route for the return value. `get()` blocks if necessary, then retrieves it. By default, the implementation may start a new thread or defer the task until the future is waited for; a launch policy can select the behavior. section 4.2.1
Listing 4.6. Using std::future to get the return value of an asynchronous task
#include <future>
#include <iostream>
int find_the_answer_to_ltuae();
void do_other_stuff();
int main()
{
std::future<int> the_answer=std::async(find_the_answer_to_ltuae);
do_other_stuff();
std::cout<<"The answer is "<<the_answer.get()<<std::endl;
}
The listing places other work between starting the calculation and retrieving its result. section 4.2.1
Choose how the event is produced and who may observe it:
- `std::future<T>` is the unique handle to an event; `std::shared_future<T>` permits multiple handles to become ready together and access its associated data. Use `void` when no result value is needed. section 4.2
- `std::promise<T>` separates production from consumption: obtain its future with `get_future()`, then call `set_value()` to make that future ready. A connection-processing thread can use this pairing to deliver a received payload or a transmission-success flag to a waiting thread. section 4.2.3
- `std::packaged_task` couples a callable to a future. It can capture a worker’s return value or exception, as in parallel accumulation, without relying on a separate shared-results array. section 8.4.1
Failure travels through the same result channel. If an `std::async` call or packaged task throws, its future becomes ready with an exception; `get()` rethrows it. A producer using `std::promise` can instead call `set_exception()`. Destroying a promise without setting a value also stores an exception. Futures transfer worker failures, but they do not by themselves ensure spawned threads are joined when other operations throw. section 4.2.4 section 8.4.1
Make one computation depend on another
In parallel quicksort, `std::async` starts sorting the lower partition and returns its future. The current thread sorts the higher partition, then calls `get()` to obtain the lower result before assembling the sorted list. The future expresses the dependency without an explicit shared result variable. section 4.4.1
A standard `std::future` requires a wait—using `wait()`, `wait_for()`, or `wait_until()`—when code must process a result only after it is ready. The Concurrency TS introduces `std::experimental::future` with `then()`: attaching a continuation invalidates the original unique future and returns a new future for the continuation’s result. Continuations can chain login operations and propagate exceptions through calls to `get()`. To avoid threads blocked on backend calls, those calls must themselves return futures that become ready asynchronously. section 4.4.3 section 4.4.4







