explaingit

hnes/libaco

Analysis updated 2026-07-03

3,684CAudience · developerComplexity · 4/5Setup · moderate

TLDR

Libaco is a tiny C library under 700 lines that implements fast cooperative coroutines, switching between tasks in about 10 nanoseconds and supporting both dedicated and shared stack modes for running millions of lightweight concurrent tasks.

Mindmap

mindmap
  root((libaco))
    What it does
      Cooperative coroutines
      Single-thread multitasking
      Fast context switching
    Performance
      10ns switch time
      10M coroutines on 2.8GB
      Under 700 lines
    Stack modes
      Dedicated stack
      Shared copy-stack
    Platforms
      32-bit x86
      64-bit x86
    API
      Create and destroy
      Resume and yield
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Replace OS threads with coroutines in a high-performance C server to handle thousands of connections with less overhead.

USE CASE 2

Run millions of lightweight concurrent tasks in a single thread using shared stack mode when memory is limited.

USE CASE 3

Use libaco as the coroutine foundation for a custom async I/O framework in C.

USE CASE 4

Study the formal correctness proof to verify the context switching implementation before using it in production.

What is it built with?

Cx86x86_64

How does it compare?

hnes/libacolinw7/skill-treeaubio/aubio
Stars3,6843,6863,702
LanguageCCC
Setup difficultymoderateeasyeasy
Complexity4/51/53/5
Audiencedeveloperdeveloperdeveloper

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · moderate Time to first run · 30min

Only supports x86 and x86_64 processors via Sys V ABI, no ARM or Windows support.

In plain English

Libaco is a small C library that lets programs run multiple tasks cooperatively within a single thread, a technique called coroutines. Normally when a program runs multiple things at once it uses threads, where the operating system decides when to switch between them. With coroutines, the code itself decides when to pause one task and hand control to another. This can be much faster because it avoids the overhead of the operating system stepping in. The library is designed for speed. The README reports that switching between coroutines takes about 10 nanoseconds on a typical cloud server when using the standalone stack mode. The entire implementation is under 700 lines of code. It currently supports the standard calling conventions (Sys V ABI) for 32-bit and 64-bit Intel processors. A key design feature is how it handles memory for each coroutine's stack. Libaco offers two options. A coroutine can have its own dedicated stack, which is fast to switch to but uses more memory. Alternatively, multiple coroutines can share a single stack, with each coroutine saving a copy of the relevant portion when it pauses and restoring it when it resumes. The shared stack approach is very memory efficient: the README demonstrates running 10 million simultaneous coroutines using only 2.8 gigabytes of physical memory when each coroutine uses a small copy-stack configuration. The README also includes a formal mathematical proof of correctness for the context switching implementation, explaining in detail how the CPU registers and stack state are saved and restored. This is unusual for a library of this type and is aimed at developers who want to verify that the underlying mechanism is sound before relying on it in production. The library is described as production-ready. The API is small: functions to create and destroy coroutines and shared stacks, and functions to resume and yield between them. The full README is longer than what was shown.

Copy-paste prompts

Prompt 1
Using libaco, write a C program that creates 1000 coroutines on a shared stack, each printing its index and yielding, to demonstrate cooperative multitasking.
Prompt 2
Show me how to integrate libaco into a C server that handles multiple client connections using coroutines instead of threads, with resume and yield calls around I/O operations.
Prompt 3
Explain how libaco's shared stack mode saves memory compared to dedicated stacks, and help me calculate the right copy-stack size for my use case.
Prompt 4
I want to build a simple async task scheduler in C using libaco. Walk me through creating a main scheduler coroutine that resumes worker coroutines in round-robin order.

Frequently asked questions

What is libaco?

Libaco is a tiny C library under 700 lines that implements fast cooperative coroutines, switching between tasks in about 10 nanoseconds and supporting both dedicated and shared stack modes for running millions of lightweight concurrent tasks.

What language is libaco written in?

Mainly C. The stack also includes C, x86, x86_64.

How hard is libaco to set up?

Setup difficulty is rated moderate, with roughly 30min to a first successful run.

Who is libaco for?

Mainly developer.

Open on GitHub → Explain another repo

This repo across BitVibe Labs

Scan in gitsafehub Deploy in gitdeployhub hnes on gitmyhub

Verify against the repo before relying on details.