Introduction
Cortex is a C++ stackful coroutine library with WebAssembly (Emscripten) support. It provides cooperative multitasking with explicit suspend/resume semantics and works across native and WASM builds through a unified API.
Highlights
- Stackful coroutines with a simple
Resume / Suspend model
- Unified API for native (Boost.Context) and WASM (Emscripten fibers)
- tiny_fiber module for single-thread cooperative multitasking (native + WASM)
- Custom memory resource support for stack allocation
- Designed for deterministic, cooperative control flow
Quick Start
});
co.Resume();
Provides a mechanism for a coroutine to suspend itself.
Definition coroutine_suspend_context.hpp:17
virtual void Suspend()=0
Suspends the current coroutine's execution.
static Coroutine Make(CoroutineBody body, std::size_t stack_size_bytes=kDefaultStackSizeBytes, MemoryResourceSharedPtr resource=GetDefaultMemoryResource())
Creates a new coroutine with the specified body and stack size.
void Resume()
Resumes the execution of the coroutine.
Main entry point for the cortex coroutine library.
tiny_fiber Module
Higher-level cooperative multitasking built on Coroutine:
tf::Scheduler::Run([] {
auto future = tf::Spawn([] {
tf::Yield();
return 42;
});
tf::Yield();
int result = future.Get();
});
Cooperative multitasking primitives built on cortex::Coroutine.
Definition condition_variable.hpp:13
Convenience header that includes all tiny_fiber components.
For WASM integration with JS event loop:
auto scheduler = tf::Scheduler::Create([]{
});
while (!scheduler.IsDone()) {
scheduler.Step();
}
Generator Example
yield(1);
yield(2);
yield(3);
});
while (gen.Next()) {
}
static Generator Make(Body body, std::size_t stack_size_bytes=262144, MemoryResourceSharedPtr resource=GetDefaultMemoryResource())
Creates a new generator with the specified body and stack size.
Definition generator.hpp:76
T DetachValue()
Moves out the current value and clears it.
Definition generator.hpp:159
Stackful generator built on top of Cortex coroutines.
API Overview
Resources