I did the clang blocks thing:
https://mikeash.com/pyblog/friday-qa-2009-10-30-generators-i...
I think it turned out pretty well, but I never used it in real code. The ability to avoid global state is great, anyway.
In embedded land, you can just make something that works like setjmp and memcopy the stack off to someplace convenient.
Super useful if you want something resembling modern programming in an embedded context. It is a bit better than callback hell, but you do end up using memory for all those saved off stacks, so that limits how many co-routines can be in play at once. Thankfully embedded projects tend to have small stacks. :)
- Use __LINE__ to make it so you don't have to manually specify the continuation number (with the caveat that you can't have more than one on a line).
- Use computed goto and labels rather than a switch and you can make it work even inside a switch (&&label to take the address, goto *addr to goto). This is a gcc extension.
- Use clang blocks to save state and return the block for continuation, saving you from needing to use static functions. Perhaps you could also use C++ lambdas along the same line, but I'm not sure if they mix well with the &&label thing.