r/cpp • u/Zealousideal-Mouse29 • Mar 11 '26
std::promise and std::future
My googling is telling me that promise and future are heavy, used to doing an async task and communicating a single value, and are useful to get an exception back to the main thread.
I am asked AI and did more googling trying to figure out why I would use a less performant construct and what common use cases might be. It's just giving me ramblings about being easier to read while less performant. I don't really have an built in favoritism for performance vs readability and am experienced enough to look at my constraints for that.
However, I'd really like to have some good use-case examples to catalog promise-future in my head, so I can sound like a learned C++ engineer. What do you use them for rather than reaching for a thread+mutex+shared data, boost::asio, or coroutines?
View all comments
Show parent comment
4
u/jwakely libstdc++ tamer, LWG chair Mar 12 '26
No, that's what
std::asyncdoes, and it gives you back astd::futureand internally uses astd::promise(or something like it) to send the result in the future. But neitherpromisenorfuturedoes anything like creating threads on their own. They are just two ends of a pipeline where you put a result in one end (the promise) and get the result out of the other (the future). Creating threads (or other execution agents) to use the promise and future is separate from the promise and future themselves.