Re:My tutorial and take on C++20 coroutines
by account4mypc on Monday February 22, 2021 @01:15PM
(#26228036)
> It seems really dumb that they are stackless
Why? C/C++ already has stackful coroutines. And that seems extremely wasteful unless you know you'll run the coroutines at the same time... with single threaded stackful coroutines, you'd get two stacks and only ever use one at a time. that wastes megabytes, requires heavier context switching, makes cache misses more likely, etc.
Re:My tutorial and take on C++20 coroutines
by hctaw on Monday February 22, 2021 @04:16PM
(#26230751)
Modern stackful coroutines don't (or shouldn't) use context switching (at least not anything like ucontext_t, just use threads if you're going to spend the cycles to preserve the signal mask) or setjmp/longjmp. Those tricks are slow and hazardous, especially when code needs to cross language boundaries or remain exception safe.
Re:My tutorial and take on C++20 coroutines
by gpderetta on Monday February 22, 2021 @10:16AM
(#26225450)
The compilation model is implementation defined, but yes, the expectation is that they compile down to a switch-like explicit state machine.
The advantage is that the worst case frame size is bounded, small and computable at compiletime.
Stackfull coroutines are of course more powerful and have, IMHO, better ergonomics.
Is there no setjmp/longjmp happening? Are C++ 20 coroutines all just compiler slight-of-hand similar to duff's device with no real context switching?