3 ms·
That is a fascinating comparison! History really does rhyme. What you described—suspending with a flag and resuming with new values—is surprisingly similar to
by lixiasky 8mo ago
That is a fascinating comparison! History really does rhyme.
What you described—suspending with a flag and resuming with new values—is surprisingly similar to how the C++20 compiler transforms coroutines into a state machine under the hood.
In tiny_coro, the promise_type essentially manages that "inversion of control" you mentioned, but hides the manual state management behind the co_await syntax. It's really cool to see that these fundamental patterns for "stopping and resuming" computation have existed for so long, just in different forms.
- throwaway81523 8mo agoAlso look at Protothreads :)
- lixiasky 8mo agoOh absolutely! Adam Dunkels' work is legendary. It's kind of poetic that Protothreads achieved stackless concurrency via C preprocessor macros and Duff's device tricks, and now years later, C++20 finally baked that exact same "state machine via switch-case" logic directly into the compiler. Same spirit, just less macro magic! :)
- srean 8mo agoSimon Tatham and Tom Duff too. https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
- lixiasky 8mo agoThe legendary "Coroutines in C" article! Thanks for the reference—I learned a lot from his work. His use of macros to hide the switch statement is basically the spiritual ancestor of co_await. It is wild to think that C++20 essentially standardized that exact pattern, just moving the "state machine generation" from the preprocessor hacks to the compiler itself.
- srean 8mo agoCoroutine idea is very old. I am not 100% sure but they were heavily used in Telecom applications. It's easier in C and old C++ if one can drop down to assembly to switch between different call stacks. Among modern languages there is Lua. Take a look at Felix too. https://felix-tutorial.readthedocs.io/en/latest/tut110.html https://felix-tutorial.readthedocs.io/en/latest/tut110.html https://felix.readthedocs.io/en/latest/fibres.html https://felix.readthedocs.io/en/latest/fibres.html
- lixiasky 8mo agoYou are absolutely right about the history (Conway coined the term in '63!). The assembly stack-switching you mentioned is the classic "Stackful" approach (like Fibers or Boost.Context). C++20 went the "Stackless" route (compiler-generated state machines) to avoid the per-stack memory overhead, though it certainly makes the implementation mechanics trickier. Lua is definitely a gold standard for asymmetric coroutines. As for Felix—I actually wasn't familiar with it until you mentioned it. Thanks for the pointer! I'll definitely dig into their docs to see how they handle scheduling compared to the C++20 approach.
- srean 8mo agoBTW what happened to your baby wireshark ? Also, do you use ChatGPT to frame your responses ? Nothing wrong if you do, but people might notice the tone and wonder.
- lixiasky 8mo agoHaha, you have a really sharp eye! 1. The Baby Wireshark: Unfortunately, my previous GitHub account got flagged/shadowbanned (still not sure why, and the appeal didn't work), so I lost access to that repo. I had to start fresh with this new account. 2. The Tone: You caught me! English is not my first language. I do use LLMs to help polish my grammar because I want to ensure my technical points are clear and I don't accidentally sound rude due to language barriers. Also, I learned most of my English from reading technical documentation and watching TED talks, so my "default" style tends to be a bit formal—just like an AI! :)