std::promise and std::future in c++

  1. Think of promise and future as creating a single-use channel for data. promise creates the channel, and eventually writes the data to it with promise::set_value. future connects to the channel, and future::wait reads and returns the data once it's been written.

  2. No real concern, because the only way to "pair" a future with a promise is with promise::get_future.


  1. They are associated by the std::promise::get_future member function. You get the std::future associated with an std::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 corresponding std::future.

  2. No, because you don't pair them after creation. You get your std::future from a std::promise, so they are inherently linked.