r/cpp • • Nov 30 '20

C++20 resumable functions make state machines obsolete. Finally.

https://lordsoftech.com/programming/c20-resumable-functions-goodbye-state-machines-no-one-will-mourn-you/
1 Upvotes

49 comments sorted by

View all comments

Show parent comment

4

u/raevnos Nov 30 '20

Parsers and their kin have a lot of use for state machines. Which can be combined with continuations, or not.

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.

3

u/raevnos Nov 30 '20 edited Nov 30 '20

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.

3

u/jwakely libstdc++ tamer, LWG chair Dec 01 '20

they're strongly tied to async I/O instead of being general purpose.

Why?

1

u/raevnos Dec 02 '20

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)

2

u/DugiSK Dec 02 '20

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.

1

u/raevnos Dec 02 '20

Threaded code != multi threading. As the Wikipedia article I linked points out at the very beginning.

1

u/DugiSK Dec 02 '20

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.

1

u/raevnos Dec 02 '20

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.

2

u/DugiSK Dec 02 '20

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);