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
14
u/robhanz Mar 12 '26
Note that it's not just convenience, but reliability and debuggability. Worrying about a trivial performance hit (in most cases, as you point out) by writing code in a way that is far more likely to introduce bugs is almost always a bad tradeoff.
Your point about server code is good, too. Is JS the most performant language? No. Is server CPU usually the bottleneck? Also no. In cases where it is, is ensuring you can scale-out probably necessary anyway? Absolutely. So the tradeoff then really gets into ability to write the code vs. additional scale-out costs, and that probably favors JS.
Performance is always contextual and is not an absolute.