std::promise and std::future in c++
Think of
promise
andfuture
as creating a single-use channel for data.promise
creates the channel, and eventually writes the data to it withpromise::set_value
.future
connects to the channel, andfuture::wait
reads and returns the data once it's been written.No real concern, because the only way to "pair" a
future
with apromise
is withpromise::get_future
.
They are associated by the
std::promise::get_future
member function. You get thestd::future
associated with anstd::promise
by calling this function.A
std::future
represents a value that you do not yet have, but will have eventually. It provides functionality to check whether the value is available yet, or to wait for it to be available.A
std::promise
makes a promise that you will eventually set a value. When a value is eventually set, it will be made available through its correspondingstd::future
.No, because you don't pair them after creation. You get your
std::future
from astd::promise
, so they are inherently linked.