liveBook · Manning

Get

As explained in C++ Concurrency in Action, Second Edition

`get()` is a future operation that retrieves the result represented by the future, waiting for it if necessary and rethrowing any stored exception.

`get()` retrieves the result represented by a future, waiting until it is ready if necessary. With an asynchronous task, this lets the caller do other work before collecting the result. 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 example starts a task with `std::async`, does other work, then calls `get()` to collect the answer. section 4.2.1

If the task throws, its exception is stored in the future; `get()` rethrows it. Continuations also use `get()` to retrieve prior results and propagate failures along the chain. section 4.2.4 section 4.4.4

How C++ Concurrency in Action, Second Edition covers it

How deep it goes
Explains waiting for readiness and retrieving values or rethrown exceptions, without detailing implementation internals.
Examples
Uses an asynchronous answer calculation, a failing square-root call, parallel Quicksort, and chained user-login tasks.
What it assumes
Assumes a future that represents a task's eventual result; introduces its use for transferring results from asynchronous work.
What you can do after
Apply `get()` to collect an asynchronous result and handle exceptions propagated through a future.
How it connects
Connects futures with `std::async`, `std::packaged_task`, `std::promise`, and continuations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C++ Concurrency in Action, Second Edition. The section links point to the parts of the book it draws on.