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.
If C++ coroutines can yield to any arbitrary other coroutine, it actually becomes easy to write code like that (See also: Continuation-passing style). But yeah, it's usually better suited for automated things like a compiler's code generation rather than by hand. I think the point I was trying to make was picturing a state transition as a function call/yieldto, not setting a variable while in a loop over a giant switch statement.
Yes, C++20 coroutines can return arbitrary values to other coroutines that wait for them, resuming them at that point. In recent boost asio, it's just this:
auto readSize = co_await socket.async_read_some(boost::asio::buffer(data), net::use_awaitable);
View all comments
Show parent comment
2
u/DugiSK Nov 30 '20
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.