Custom coroutines implementation in GNU C.
Coroutine is a lightweight user space thread with its own stack that can suspend its execution and switch to another coroutine on demand. Coroutines do not run in parallel but rather cooperatively switch between each other whenever they feel like it.
Coroutines are useful in cases when all your program does majority of the time is waiting on IO. So with coroutines you have an opportunity to switch the context and go do something else. It is not useful to split up heavy CPU computations because they all going to be executed on a single thread. Use proper threads for that (pthreads on POSIX).
Good use cases for coroutines are usually Network Applications and UI. Anything with a slow Async IO.
See coroutine.h for more info. See ./examples/counter.c for a simple usage example in C.
To build the example:
$ make
$ ./build/counter
There are actually much more examples in the ./examples/ in a variety of languages. To build all of them do:
$ make examples
Make sure you have all the corresponding compilers for the languages.
- Linux x86_64
More are planned in the future