Exploring promises and futures
A future is an object that represents some undetermined result that will be completed sometime in the future. A promise is the provider of that result.
Promises and futures have been part of the C++ standard since version C++11 and are available by including the <future>
header file, promises via the class std::promise
and futures via the class std::future
.
The std::promise
and std::future
pair implements a one-shot producer-consumer channel with the promise as the producer and the future as the consumer. The consumer (std::future
) can block until the result of the producer (std::promise
) is available.
Figure 6.1 – Promise-future communication channel
Many modern programming languages provide similar asynchronous approaches, such as Python (with the asyncio
library), Scala (in the scala.concurrent
library), JavaScript (in its core library), Rust (in its Standard Library (std) or crates such as promising_future...