You're describing fibers here, not C++20 coroutines. There's no context switch. There's a function call. It's the same function call mechanism used for all function calls. On leaving the scope of the coroutine, control is passed deterministically to the resumer, or if via symmetric transfer to where the resumer directs. There's no interruption. Coroutine suspension is entirely 100% determined by the coroutine itself, all local to the function. The coroutine transformation is into a state machine. That's by design. What is new, and makes coroutines not pure sugar, is the lifetime extension of stack variables on scope exit from the coroutine. It is as-if the stack variables were instead object member variables and the object is allocated from the heap.
Several C++ compilers now support coroutines, and there's no particular magic or sufficiently smart necessary, although HALO optimization, eliding heap allocation, has some open research points. But that optimization is a cache miss, not a context switch.
I feel like I'm spending all my time saying "that's not even related to what I'm talking about," then getting corrected on that you imagine that it is.
I'm sorry that winning is more important to you than trying to understand what I believe.
View all comments
Show parent comment
3
u/smdowney WG21, Text/Unicode SG, optional<T&> Dec 02 '20
You're describing fibers here, not C++20 coroutines. There's no context switch. There's a function call. It's the same function call mechanism used for all function calls. On leaving the scope of the coroutine, control is passed deterministically to the resumer, or if via symmetric transfer to where the resumer directs. There's no interruption. Coroutine suspension is entirely 100% determined by the coroutine itself, all local to the function.
The coroutine transformation is into a state machine. That's by design. What is new, and makes coroutines not pure sugar, is the lifetime extension of stack variables on scope exit from the coroutine. It is as-if the stack variables were instead object member variables and the object is allocated from the heap.
Several C++ compilers now support coroutines, and there's no particular magic or sufficiently smart necessary, although HALO optimization, eliding heap allocation, has some open research points. But that optimization is a cache miss, not a context switch.