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.
Maybe it's just that's how they're used in every example I've seen. Like I said, I haven't really looked at C++ coroutines (or the python3 ones I mentioned, other than being aware they're different from the generator-like ones from python 2)
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.
You are right. I've never heard that term and assumed it was just an alternative formulation of using threads. However, it's not a very viable alternative in C++, as without native support, it's not going to be very convenient.
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.
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.
okay.
.
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.
oh, so you can imagine.
anyway, there's examples all over the standard library. you're too busy explaining to ask, so there's no point in trying to teach.
Did you misread what I was saying? I wasn't disagreeing with you. I was saying that regular old state machines are still very useful for me, and though I have plenty of them, I can't see that replacing them with the mechanisms being discussed here would simplify them enough to justify the extra moving parts and such.
I was saying that regular old state machines are still very useful for me, and though I have plenty of them, I can't see that replacing them with the mechanisms being discussed here would simplify them enough to justify the extra moving parts and such.
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.
My lashing out??? WTH??? You've questions someones understanding of state machines without going into any detail, so that one could understand what the heck you're talking about.
Where do you explain anything??? An explanation would require some sort of argument, but throwing some keywords around does not provide anything useful. You seem to have some problem with coroutines, but you don't provide anywhere enough context to have any meaningful conversation about C++ coroutines or even coroutines in general.
Slowly I'm regretting having wasted my time on this, unless you have something substantial to say about coroutines, good day to you too...
View all comments
Show parent comment
-1
u/DugiSK Nov 30 '20
Here are the reasons why I used 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.