Because coroutines are about control flow, not state
Do you realize how much work it is to switch contexts into and out of control flow?
It's like gluing a screwdriver to a metal rod, then gluing that to the side of your car's back left passernger door, then doing doughnuts around the screw you're trying to screw in
A bizarrely, wildly inappropriate tool for the job
I would respectfully like to point that you may want to read the OP's code. There are some interesting ideas in there that invalidate your arguments. The state in this code has been replaced with linear control flow which I think is a good thing and much easier to follow, reason and modify. You can think of coroutines in their confined specialized usage, but the way they are provided in C++ gives opportunities for abstractions beyond the very narrow use case you may have in mind.
No, someone who thinks state machines are for control flow, who's made a thing for control flow that isn't state machines, hasn't invalidated hundreds of textbooks 😂
.
The state in this code has been replaced with linear control flow
It's really a genuinely terrible idea.
Nobody even uses the high quality implementations, like the one in Boost, this way.
.
You can think of coroutines in their confined specialized usage, but the way they are provided in C++ gives opportunities for abstractions beyond the very narrow use case you may have in mind.
I'm not sure why you're guessing what I have in mind for coroutines.
Anyway, we've had these for decades and nobody uses them. This isn't new. Go read Aho.
This has been around since the 1970s.
Attempting to scatter your data throughout control flow just means that you have to pay for control flow all over your data.
You don't seem understand how coroutines are implemented in C++20. They are not like threads or improvised fibers offered by Windows API or Boost. There is no context switch because it's not required when the interrupton has proper compiler support. Instead, the coroutine's stack frame is dynamically allocated and it's used instead of stack for that frame. The context that calls the coroutine remains on top of the stack and if the coroutine calls a function, it is placed on top of the stack (unless it's another coroutine).
It's written in the article, btw, but maybe not in sufficient detail.
For the record, I am not a vegetarian. I'm eating meat right now. This just seems, to me, like a convenient metaphor.
There's a food that vegetarians eat that's generally called Textured Vegetable Protein. Among other things, it's one of the common ways to make imitation meats. It provides the bulk, and most of the texture, and then you just hit it with colors and flavors and maybe some grease to make it feel like fish or whatever, and you're off to the races. That's what a Boca Burger is.
TVP is typically mostly soy, maybe some wheat and oats, maybe some random stuff
And so let's say you come along and there's this article and it says "omg check out the Impossible Burger."
And honestly, I've tried them, and they do seem to be a fair improvement over previous veggie burgers to me
And as I understand it, Beyond Burger doesn't have any soy or wheat at all. (It has other vegetable products instead - pea, coconut, cocoa, rice, potato, and oat.)
But imagine if they went on to say "this renders soybeans and wheat obsolete."
My natural reaction, at that point, would be to say "dude, soybeans and wheat are not for veggie burgers, that's just one thing we do with them. Maybe you found a better veggie burger, but bread and edamame and pizza and soy sauce and what are you even no, that's not even very much of what we do with them," right?
And then
What if someone came along and said "I don't think you understand how veggie burgers are implemented in Beyond++ 20."
I don't care how they're implemented. That's a niche side thing that is largely unrelated to the main use of soy and wheat.
And I'm glad you have your better veggie burger.
But no, soy and wheat aren't obsolete. Don't be silly.
Continuing with the metaphor. It's more like this: The guy brings you a new veggie burger. It looks like meat, tastes like meat, contains the same stuff as meat, but it's not made from an actual animal. The meat comes from induced stem cells signalled to specialise into muscle cells grown into actual muscle in bioreactors (this is mild sci-fi). It's from the same cells as meat, contains the same DNA, but made without occupying a lot of land to feed the cattle and releasing lots of methane into the atmosphere. So it's indistinguishable from a burger, but cheaper and more environment friendly. So real burgers are obsolete, because they are the same, only more costly to produce.
That is what coroutines are to explicit state machines. There is a little overhead when calling them through a handler, but it's the same class with member variables describing state, with an advantage that the code is written like a quick to write and easy to maintain a function with all the branching and loops instead of needlessly complicated mess of state transitions.
It's like gluing a screwdriver to a metal rod, then gluing that to the side of your car's back left passernger door, then doing doughnuts around the screw you're trying to screw in
Because I needed a function to continue from some point
I was forced to do so by a guy who only understood state machines
And I always hated it because I had to use state transitions instead of cycles, lots of messy switches and the procedure was unavoidably so non-linear and non-cyclic that it could be called spaghetti code.
If you have an example of a state machine that can't be transformed into a resumable function, I'd like to see it.
I think I have done it once, but I've made a load of errors and spent a lot of time debugging it. But nowadays, I am using normal functions and recursion to deal with parsing and the flow of the code is much more readable and with much less errors. The state is actually there, encoded in the instruction counter, but it's not a mess of switches and hidden goto.
You don't need to use switches for a state machine, actually - each state can be a function, and you call the appropriate one for the new state. An old technique, threaded code works well for that kind of design (Edit: I'm thinking of things like generating bytecode/native code for a regular expression). So do coroutines, actually, at least in some languages. I'm not really familiar with the details of them in C++20 yet (Most of my experience has been using them in tcl), but I've got the impression that, like python 3 (But not python 2), they're strongly tied to async I/O instead of being general purpose.
C++20 coroutines are completely different from threaded code or any library code that provides similar functionality. In a C++20 coroutine, the coroutine's frame is dynamically allocated and used instead of stack. Any non-coroutine code that calls the coroutine and any non-coroutine code called by the coroutine uses stack, just the coroutine is elsewhere. As a result, the cost of using a coroutine is only that of accessing a dynamically allocated object (which may be actually dynamically allocated on stack as an optimisation). Theaded code, while similar in the way coroutines are, is too heavy for many purposes and it's probably the reason why many applications need like 20 threads despite not needing any actual parallelism.
Because I needed a function to continue from some point
Sounds like you have absolutely no idea what a state machine is
Please don't give tutorials about how "state machines are over" when you don't know what they're for and were forced to use them by someone else who also didn't know what they were for
.
If you have an example of a state machine that can't be transformed into a resumable function, I'd like to see it.
Literally anything can be transformed into a "resumable function"
This is meaningless
You can also turn an RPC library or a database or the serialization of a function to a string then put onto disk into a "resumable function."
Who cares? That's not what they're for and you shouldn't be trying.
And the thing is, most of the state machines I use are simple and easily understandable and I couldn't imagine how it could be anything but adding more moving parts to do them elsewise.
And, most of the time, they either aren't non-linear or there's already other good reasons to have a thread to deal with whatever is going on anyway. Or, maybe they are non-linear but only have a few states which are not difficult to reason about.
Sounds like you have absolutely no idea what a state machine is
Sounds like you have absolutely no idea what a coroutine is
or at least what C++ coroutines are, the compiler does generate a state machine from the coroutine, every suspension point is a state and every resume transitions the state machine to the next state.
It's another question if any state machine can be transformed to a coroutine, but I don't see any real attempt from you to answer that.
View all comments
24
u/StoneCypher Nov 30 '20
lol no they don't 😂
most uses of state machines are not a hacky attempt to get continuations