finds.dev← search

// the find

cloudwu/coroutine

★ 2,521 · C · MIT · updated Dec 2022

A asymmetric coroutine library for C.

A minimal asymmetric coroutine library for C, written by cloudwu (creator of skynet). It implements coroutines using a shared-stack model with ucontext, letting you spin up many coroutines without pre-allocating per-coroutine stacks. Aimed at C developers who want lightweight cooperative multitasking without pulling in a full framework.

The shared-stack design is the interesting part: all coroutines in a schedule share one stack, and the live portion is copied out on yield and restored on resume. This keeps memory usage flat regardless of coroutine count. The implementation is under 200 lines — readable in an afternoon. It comes from the author of skynet, so the design choices are battle-tested in a production game server context.

Stack copying on every context switch is a correctness trade-off: anything holding a pointer into the stack across a yield is a dangling pointer waiting to happen, which is a non-obvious footgun in C. The library hasn't been touched since 2022 and is ucontext-based, which is POSIX-only — no Windows support without significant surgery. No error handling to speak of: bad inputs or OOM just crash or corrupt silently. At 5 files total, this is a learning artifact more than a production library — if you need coroutines in real C code, libco or the stackful coroutine support in frameworks like libuv are better-maintained alternatives.

View on GitHub →

// want more like this?

We dig through GitHub every week and send a few repos picked for what you actually care about — each with an honest take like this one.

Get finds in your inbox → Search again →