3 ms·
Article is pretty good but C++ coroutines sound like a contorted version of protothreads. Not that useful in the first place, and made way more painful for the
by throwaway81523 4y ago
Article is pretty good but C++ coroutines sound like a contorted version of protothreads. Not that useful in the first place, and made way more painful for the sake of enabling some hypothetical compiler optimizations that aren't done in practice. The two specific pain points are
1) stacklessness, which means coroutines can only yield from the top-level function that is called by the coroutine, sort of like the "simple generators" introduced in Python 2. Some people would consider these to not be real coroutines at all, and Python 3 later introduced actual (stackful) coroutines aka async functions. The C++ devs say that if their coroutines had stacks they would be called fibers instead of coroutines, but I had always thought fiber (used that way) was a Windows-specific term.
2. Opaqueness of the coroutine frame object: you can't get at its size or contents. That lets the compiler optimize its layout or even in some cases eliminate it. But at least for now, real compilers don't seem able to do that, and s0me LLVM devs aren't sure it's even feasible. Rust doesn't attempt this. It just uses an ordinary struct to hold the frame data, so you can do normal struct things with it.
Above is my impression from reading the linked article and some related ones. I have not yet tried to use C++ coroutines, or looked into Rust's even slightly. Protothreads (a C header library) are cool though. They don't do that much, but they are very simple in implementation compared to this C++ mess.
I think these C++ coroutines don't reach the usefulness of a lightweight cooperative Forth multitasker from 50 years ago. Those were stackful (typically a few dozen cells in each stack, so a task cost on a 16-bit cpu might have cost 200 or so bytes including stacks, user variables, and other data) and ridiculously simple.
Goroutines aren't comparable. They are way more powerful but also way more expensive than C++ coroutines.
There were apparently some other C++ coroutine proposals that sounded a lot better, but that didn't have the backing to get through the standardization process.
I don't know how Boost coroutines compare. I tried to use them once but didn't get them working.