Daily bit(e) of C++ | std::promise, std::future
Daily bit(e) of C++ ♻️26, High-level synchronization tools that implement one-shot consumer-producer semantics: std::promise and std::future.
The std::promise and std::future are high-level synchronization tools that implement one-shot producer-consumer semantics.
A producer can fill the state of a std::promise with either a value or an exception. A consumer holding a std::future associated with this std::promise can call get() to block until the shared state is available, at which point get() will either re-throw the exception or return the value.
Note that the shared state can only be accessed once.
std::future also supports timeout/deadline through the wait_for() and wait_until() methods.