liveBook · Manning

Std::promise

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

`std::promise` is a C++ standard library class template that stores a value or exception in a shared state for an associated future to retrieve.

A `std::promise<T>` lets one part of a program explicitly place a value of type `T` into shared state for an associated `std::future<T>` to retrieve. Call `get_future()` to obtain that future; calling `set_value()` makes it ready. This is useful when a result arrives independently of a simple function call, such as data received while a thread handles multiple network connections. section 4.2.3 section 4.2.2

Listing 4.10. Handling multiple connections from a single thread using promises
#include <future>
void process_connections(connection_set& connections)
{
    while(!done(connections))                            #1
    {
        for(connection_iterator                          #2
                connection=connections.begin(),end=connections.end();
            connection!=end;
            ++connection)
        {
            if(connection->has_incoming_data())          #3
            {
                data_packet data=connection->incoming();
                std::promise<payload_type>& p=
                    connection->get_promise(data.id);    #4
                p.set_value(data.payload);
            }
            if(connection->has_outgoing_data())          #5
            {
                outgoing_packet data=
                    connection->top_of_outgoing_queue();
                connection->send(data.payload);
                data.promise.set_value(true);            #6
            }
        }
    }
}


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":3,\"ch\":57},{\"line\":3,\"ch\":59}],[{\"line\":5,\"ch\":57},{\"line\":5,\"ch\":59}],[{\"line\":10,\"ch\":57},{\"line\":10,\"ch\":59}],[{\"line\":14,\"ch\":57},{\"line\":14,\"ch\":59}],[{\"line\":17,\"ch\":57},{\"line\":17,\"ch\":59}],[{\"line\":22,\"ch\":57},{\"line\":22,\"ch\":59}]]"}
!@%STYLE%@!

A connection-processing loop uses promises to deliver received packet payloads and outgoing transmission results to code waiting on their futures. section 4.2.3

A promise can make its future ready with either a value or an exception:

  • Call `set_value()` to provide a result; the associated future becomes ready and can retrieve it. section 4.2.3
  • Call `set_exception()` to store an exception for the future to rethrow on `get()`. Destroying an unfulfilled promise stores a `broken_promise` error if the shared state is not already ready. section 4.2.4

Use a promise when the result must be supplied explicitly rather than returned directly by a task. `std::packaged_task` provides a higher-level alternative for associating a callable task with a future. section 4.2.1 section 4.2.2

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

How deep it goes
Defines `get_future`, `set_value`, and exception delivery, without developing a full API reference.
Examples
Demonstrates network packet delivery, transmission success flags, and explicitly stored exceptions.
What you can do after
Apply `get_future`, `set_value`, and `set_exception` to deliver results or errors to waiting code.
How it connects
Pairs `std::promise` with `std::future` and contrasts it with `std::packaged_task` and `std::async`.

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.