3 ms·
This is a fascinating concept that I've written about before: "C#’s async/await compared to protothreads in C++". [1] Both async/await in C# and protothreads [2
by benhoyt 3y ago
This is a fascinating concept that I've written about before: "C#’s async/await compared to protothreads in C++". [1] Both async/await in C# and protothreads [2] in C unroll control flow to a state machine. In C, this is done using some simple but crazy macros using the fact that you can interleave switch statements with other control flow (ala Duff's Device [3]).
Many years ago I ported Adam Dunkel's protothreads library to C++ [4]. You can use C protothreads in C++ directly, of course, but it's a bit nicer to use classes.
[1] https://blog.brush.co.nz/2012/11/async-await-protothreads-cpp/ https://blog.brush.co.nz/2012/11/async-await-protothreads-cp...
[2] http://dunkels.com/adam/pt/ http://dunkels.com/adam/pt/
[3] https://en.wikipedia.org/wiki/Duff%27s_device https://en.wikipedia.org/wiki/Duff%27s_device
[4] https://github.com/benhoyt/protothreads-cpp/ https://github.com/benhoyt/protothreads-cpp/