edubart/minicoro
FreeSingle header stackful cross-platform coroutine library in pure C.
About edubart/minicoro
Minicoro is a single-file library for using asymmetric coroutines in pure C. It provides stackful coroutines with support for nesting (resuming a coroutine from another coroutine), custom allocators, a storage system to pass values between yield and resume, customizable stack sizes, and growable stacks via a virtual memory allocator for low memory footprint. The library is implemented via assembly, ucontext, or fibers, making it cross-platform for Linux, macOS, Windows, Android, iOS, WebAssembly, and more. Designed to work in multithread applications, it is minimal, self-contained, free of external dependencies, and compatible with most C89 compilers. The API is inspired by Lua coroutines and returns proper error codes to aid debugging. It also supports running with Valgrind, AddressSanitizer, and ThreadSanitizer.
Key Features
Pros & Cons
- Single header file makes integration simple
- Cross-platform support via multiple backends (assembly, ucontext, fibers)
- High performance due to efficient assembly implementation
- Custom allocator support for fine-grained memory control
- Growable stack option via virtual memory reduces memory footprint
- Error-prone API with clear error codes helps debugging
- Works with common sanitizers (Valgrind, ASan, TSan)
- Compatible with most C89 compilers for legacy projects
- Not recommended for use with C++ exceptions (may not behave as expected)
- The mco_coro object is not thread safe; requires mutex protection in multithread apps
- Default stack size is limited (56KB), though adjustable
- Avoid using thread_local inside coroutine code due to potential pointer caching issues
- On WebAssembly requires -s ASYNCIFY=1 compilation flag
- WebAssembly Binaryen asyncify method fails on some toolchains like WASI SDK