Packaging up tasks for later
Another thing to notice about the preceding diagram is that the shared state doesn't just contain an optional<T>
; it actually contains a variant<T, exception_ptr>
(for variant
and optional
, see Chapter 5, Vocabulary Types). This implies that not only can you shove data of type T
through the wormhole; you can also shove exceptions through. This is particularly convenient and symmetrical because it allows std::future<T>
to represent all the possible outcomes of calling a function with the signature T()
. Maybe it returns a T
; maybe it throws an exception; and of course maybe it never returns at all. Similarly, a call to f.get()
may return a T
; or throw an exception; or (if the promise-holding thread loops forever) might never return at all. In order to shove an exception through the wormhole, you'd use the method p.set_exception(ex)
, where ex
is an object of type std::exception_ptr
such as might be returned from std::current_exception()
inside...