naasking/async.h logo

naasking/async.h

Free

Stackless Async Subroutines for C

FreeFree tier
Type
Open Source

About naasking/async.h

Async.h is a header-only C library that provides stackless asynchronous subroutines inspired by protothreads and async/await patterns from C#, Rust, and JavaScript. It is built on Duff's device and requires only 2 bytes of state, making it extremely lightweight and portable. The API includes async_begin, async_end, async_yield, await, await_while, async_exit, async_call, async_init, and async_done, enabling cooperative multitasking in embedded or resource-constrained environments. It supports nested async calls with fork-join semantics and is independent of any operating system.

Key Features

100% pure portable C, header-only implementation
Very low state requirement (~2 bytes)
Not dependent on any operating system
Simpler than protothreads due to caller-saved state
Supports async_begin, async_end, async_yield, await, await_while, async_exit, async_call, async_init, async_done
Allows nested async calls with fork-join parallelism

Pros & Cons

Pros
  • Extremely lightweight (2 bytes of state per async)
  • Header-only, no compilation needed, just include
  • Portable across platforms and compilers
  • No OS dependency; works on bare metal
  • Clean, concise API inspired by modern languages
Cons
  • Limited documentation and examples beyond the README
  • No built-in scheduler; manual management of async states required
  • Not suitable for CPU-bound parallel tasks; designed for cooperative concurrency only

Best For

Embedded systems with limited resourcesCooperative multitasking in C programsState machine implementation without full-blown threadingEvent-driven programming where blocking is not acceptable