This is a C++ coroutines framework( <a href="https://github.com/markpapadakis/coros-fibers" rel="nofollow">https://github.com/markpapadakis/coros-fibers</a> ), but its trivial to “port” it to C.<p>I ‘ve also written a few things about coroutines and fibers(e.g <a href="https://medium.com/software-development-2/coroutines-and-fibers-why-and-when-5798f08464fd#.qz59082w0" rel="nofollow">https://medium.com/software-development-2/coroutines-and-fib...</a> and <a href="https://medium.com/software-development-2/high-performance-services-using-coroutines-ac8e9f54d727#.1s8lfoswb" rel="nofollow">https://medium.com/software-development-2/high-performance-s...</a> ).<p>Coroutines based on stack manipulation(as opposed to simple FSMs) are extremely powerful and are particularly useful for implementing fair scheduling across runnable ‘processes’ and concurrent execution contexts.
Fair scheduling in particular can be really important in the domain of distributed systems that need to process potentially long-running and/or expensive request which would otherwise stall processing other cheaper requests indefinitely.
Combined with async I/O, this gives near optimal concurrency and resources utilisation.
Coroutines are often associated (literally and figuratively) with futures/promises. See SeaStar framework for a great implementation of those ideas.
I just created a submission for libco, which I think is the best (fastest and most portable) library for coroutines in C. It's great, I wish it got more love. <a href="https://news.ycombinator.com/item?id=13199581" rel="nofollow">https://news.ycombinator.com/item?id=13199581</a>
I've recently made a similar protothread/coroutine library for embedded systems where local continuations can be either setjmp or goto or case labels. Nothing fancy, but perhaps someone find it useful: <a href="https://github.com/zserge/pt" rel="nofollow">https://github.com/zserge/pt</a>
This is a rediscovery of contiki's protothreads:<p><a href="http://contiki.sourceforge.net/docs/2.6/a01802.html" rel="nofollow">http://contiki.sourceforge.net/docs/2.6/a01802.html</a>
That appears as a variant of Simon Tatham idea:
<a href="http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html" rel="nofollow">http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html</a><p>Just in case, I've made C++ variant of it:
<a href="https://www.codeproject.com/Tips/29524/Generators-in-C" rel="nofollow">https://www.codeproject.com/Tips/29524/Generators-in-C</a>