cloudwu/coroutine logo

cloudwu/coroutine

Free

A asymmetric coroutine library for C.

FreeFree tier
Type
Open Source

About cloudwu/coroutine

A lightweight asymmetric coroutine library for C, inspired by Lua coroutines. It provides a schedule-based API with functions like coroutine_open, coroutine_create, and coroutine_resume. Coroutines within the same schedule share a single stack, allowing the creation of many coroutines with low memory overhead. Context switching copies the stack used by the coroutine. The library is open source under the MIT license and includes a Chinese blog post by the author for additional details.

Key Features

Asymmetric coroutine model (like Lua)
Stack sharing among coroutines in the same schedule
Lightweight with low memory overhead
Schedule-based API: coroutine_open, coroutine_create, coroutine_resume
Context switching via stack copying
Open source under MIT license

Pros & Cons

Pros
  • Stack sharing allows many coroutines without memory worry
  • Lightweight and simple API
  • MIT license permits wide usage
  • Asymmetric model similar to Lua, familiar to many developers
Cons
  • Context switching copies the stack, which may introduce performance overhead
  • Cannot call coroutine_resume from within a coroutine in the same schedule
  • Single schedule per thread (not preemptive, cooperative only)
  • Stack sharing may lead to issues if coroutines have large local state

Best For

Concurrent programming in CImplementing cooperative multitaskingAsynchronous I/O patterns

FAQ

What programming language is this library written in?
The library is written entirely in C (98.5% C, 1.5% Makefile).
What is the license?
The library is released under the MIT license.
How does stack management work?
Coroutines in the same schedule share a single stack. Context switching copies the stack of the coroutine being switched to, which allows many coroutines without high memory overhead.
Can I call coroutine_resume from within a coroutine?
No, you cannot call coroutine_resume from a coroutine that belongs to the same schedule. You must call it from the thread that opened the schedule.
Is there any documentation or blog post about this library?
Yes, the author wrote a Chinese blog post at http://blog.codingnow.com/2012/07/c_coroutine.html for additional details. The source code itself is also documented with comments.