r/cpp • u/germandiago • Jun 11 '26
Still amazed every time I read this paper. What pros and cons do you think it would have against C++20 coroutines?
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4453.pdfJust was reading again this. What I like is the core idea that it looks much closer to concepts I understand in C++ (mainly function objects) instead of all the dance for promise_type/suspend/await_transform, etc.
81
Upvotes
31
u/manni66 Jun 11 '26
"Stackless vs. Stackful Coroutines" - isn't that discussed a thousand times already? Do you think you get new insights?
4
u/kammce WG21 | πΊπ² NB | Boost | Exceptions Jun 11 '26
So I took a look at this and also fed it into Claude to discuss a bit further. Its also quite old so no real reason to nit pick it too deep. I wanted to be transparent that I didn't read the entire thing as its quite long. From what I can gather, this looks less like async code and more like syntactic sugar for a state machine. Which is a cool idea. But when I started to peer into what would be necessary in order to get concurrent operations to work, like two tasks, the examples started to throw in mutexs and threads and the like. I feel like its actually more complicated to write async code with this paradigm than with C++20 coroutines.
If your state has been allocated on the stack, as this proposes, then you're only route to doing other work while waiting for a response from something, is to use threads. And if thats the case, we already have APIs for that from C++11 which is
std::threadandstd::this_thread::yield()What I want from my async framework, that C++20 coroutines gives us, is a way to abstract over callbacks and events such that when an operation reaches a point of suspension (waiting for interrupt or callback), the operation returns control to a scheduler. Specifically, I want this without the need of a full CPU context switch. Thats the feature you get with symmetric transfer which missing from this design. And you can recreate the stack-like allocation scheme for coroutine frames at the cost of having to pass the stack down each level.
And all of this allocates the memory on the
stack_and_contextobject's stack allocated array memory. If you want more concurrent operations, make more stacks. Eventually, it all comes down to stacks OR you blocks OR the general heap to store your async operations states as they wait for I/O to do its thing. And the allocation is as you'd expect. If C returns because its done, it deallocates its from thestack_and_contextinternal array by setting its stack pointer back to it's own memory address. Like a stack normally behaves.I'll stop here because I've written a lot.
8
u/GoogleIsYourFrenemy Jun 11 '26
Python has some interesting additions to generators.
Python includes an adapter to create RIIA style scope guards from generators (to be used with a with clause). Including passing exceptions through to the generator.
The generator can catch errors thrown during the yield and the caller can pass a value back into the generator via the resume (meaning the yield in the generator returns a value to the generator, and can be called from a try catch block.).
It's worth looking at to appreciate the complexity of the design.
1
u/germandiago Jun 11 '26
It is RAII, hehe. Anyway, you mean this:
1
u/jwakely libstdc++ tamer, LWG chair Jun 15 '26
It would have been helpful to mention "Resumable Expressions" or "stackless coroutines" somewhere in your post.
1
u/csb06 Jun 11 '26 edited Jun 12 '26
There was a response from the proposers of what became C++20 coroutines at the time. I think this excerpt from P0171R0 is worth considering:
1
u/no-bugs Jun 13 '26
IMNSHO, what they claim as their main advantage (lack of co_await) is actually their main DISadvantage. The thing is that coroutines (and resumable expressions) are inherently subject to "sudden state changes" which (unlike with multithreading, phew) can happen only at the points of potential suspension. And marking these points of potential suspension (where those "sudden state changes" can happen) - is important for readability and reasoning about coroutines. For more details, see my talk at CppCon17 re. "8 way to handle non-blocking returns" (it doesn't analyze resumable expressions, but discusses the phenomenon).