•
ahartmetz 9 hours ago
This seems to omit the main point of it, though? That the compiler turns a function into a state machine that can restart from any of its suspension points. I guess that's the "state" variable, but what I'm talking about is what it contains and the partially compiler-generated code that deals with it.

C++ async is unfortunately a "bring your own runtime" sort of model (for now), but it does handle the part that would be incredibly tedious by hand, building above mentioned state machine.

•
jcranmer 9 hours ago
That's the co_await/co_yield/co_routine stuff, which this isn't talking about--this is talking about the std::future-et-al logic, which predates C++ coroutines by almost a decade.
•
kccqzy 9 hours ago
You are confusing it with coroutines. This is just a plain library feature that the compiler doesn’t necessarily have special support for.
•
jandrewrogers 8 hours ago
If the objective with using async is performance and scalability then you should almost always "bring your own runtime". Bringing your own runtime is the main reason you might want to use async.

Delegating the runtime nerfs many of the benefits that async can offer. If you are going to delegate the runtime it offers relatively little compared to just using threads.

•
larodi 2 hours ago
how's this in the news, I mean I don't get it. C++ had futures for at least 10 years now?
•
alexeiz 8 hours ago
C++ async model is too complicated (and not cost free as claimed, since it allocates memory for the coroutine state) and as such it wasn't widely adopted. I don't see any code using C++ async (apart from libraries implementing "tasks" in various forms). At this point I view it as a failed experiment. My bet is that executors are going to replace async completely.
•
pjmlp 3 hours ago
Nah, it was originally based on .NET async/await model, having been proposed by Microsoft, and initially experienced in WinRT/UWP async runtime.

So anyone that has experience in such model, including the awaitable magic methods, can grasp it.

While not cost free, the optimisers are expected to elide the cost most of the time.

•
jandrewrogers 8 hours ago
> since it allocates memory for the coroutine state

Heap allocation is not required, just the default.

You can do static allocation if you wish, though it is a little more complicated than typical static allocation due to the relative unpredictability.

•
gsliepen 7 hours ago
> The wait_for method is why we have a condition variable at all. If it weren’t for that guy, we could have used std::atomic‘s wait and notify_all methods.

No, the std::mutex is the reason you have a condition variable. Never mix std::atomic with std::mutex-and-friends, you are very likely introducing race conditions that way.

•
lukaslalinsky 6 hours ago
I think you are misunderstanding what the article suggests. std::atomic::wait and std::atomic::notify_all are the C++ abstractions over futexes. The whole concept of the future can be expressed in terms of atomics and futexes. The only problem is that the C++ standard doesn't have std::atomic::wait_for.
•
gsliepen 5 hours ago
You can indeed, but it would change the code from the article drastically: you have to get rid of the mutex, the discriminant becomes an atomic variable, and you need barriers between access to the discriminant and to the value the source is holding. Performance-wise I don't think it will show a drastic improvement, the main benefit would be the size of the source.
•
thefourthchime 9 hours ago
Wow, I had no idea Raymond Chen was still doing blog posts. I used to love his posts back in the day.
•
afdbcreid 9 hours ago
He writes regularly. Sometimes explaining C++, rarely explaining C#, sometimes historic stories, sometimes current stories, etc..