r/cpp • • Oct 14 '26

C++ futures part 2: The mental model for std::promise, std::packaged_task

https://devblogs.microsoft.com/oldnewthing/20261013-00/?p=112769/
32 Upvotes

9 comments sorted by

7

u/xaervagon Oct 14 '26

I always thought packaged_task seems cool, but never actually found a practical use for it versus other options

5

u/Flimsy_Complaint490 Oct 14 '26

i never found a use for any of this actually.

Coroutines IMO obsoleted them all.

3

u/VAbsoluteZero Oct 14 '26

In what way? These solve separate practical problem of a expressing result of - for instance - separate thread calculation.

4

u/Flimsy_Complaint490 Oct 14 '26

In that coroutines allow you to express every semantic std::future, std::async and std::packaged_task that i can conceive of, can, but more flexibily, and the async code tends to be easier to follow.

Even your example - seperating the result from compute. You can carry a std::packaged_task, or a coroutine frame object you co_await when required. but co_await is far more flexible than what the get() function can do. A coroutine frame is basically equivalent std::packaged_task on the high level. The frame also can double as a promise.

The place where I am technically incorrect is that c++20 coroutines are just compiler machinery and you need an actual runtime that expresses those semantics and allows you to compose them. But if you have that, and there are coroutine frameworks to choose today, i consider std::future, std::async and std::packaged_task largely obsolete.

Unless you are implementing a runtime yourself, then they likely have a use inside.

3

u/PossibilityUsual6262 Oct 14 '26

I have a question, in your job you actually use all of those features like in real project?

In all projects i was a part of anything internal ever, they like cpp 17 barely 20 and all async stuff written with local wrappers and nothing of std actually used.

4

u/LB-- Professional+Hobbyist Oct 15 '26

We're using coroutines in production across multiple codebases, including an old codebase stuck on an old compiler version that predates C++20. Coroutines simplify things so much compared to the old rats nest we had with future/promise and callbacks, especially since the compiler manages the coroutine frame layout and thus allows us to have the entire lifetime's worth of memory allocated up front where allocation failure is easier to handle. A ton of error situations vanished or became easier to handle when switching to coroutines.

3

u/Flimsy_Complaint490 Oct 15 '26

i dont write a coroutine framework every time if you were asking that, i get a third party one, which is either ASIO or ASIO and tmc now.

But yes i am a heavy user of coroutines. When i do C++, i mostly deal in stuff that's network interfacing, and coroutines are IMO, the objectively superior way to do asynchronious networking.

3

u/realestLink Oct 14 '26

Interesting. I've never heard of std::packaged_task before

3

u/SyntheticDuckFlavour Oct 14 '26

I've used for a custom task queue project myself. It's a pretty cool little object.