Coroutines in Capy
Capy’s coroutine model is built around a single principle: asynchronous code should look like synchronous code. The code reads top to bottom, with local variables and normal control flow. Capy handles suspension, resumption, thread scheduling, and cancellation behind the scenes. The result is code that is both easier to read and harder to get wrong.
What This Section Covers
-
The task Type — Declaring, returning values from, and awaiting
task<T>coroutines. -
Starting Coroutines — Starting coroutines with
run_async, and binding child tasks withrun. -
Executors and Execution Contexts — Executors, execution contexts, thread pools, and strands.
-
The IoAwaitable Protocol — How the executor and stop token propagate through a chain of awaited coroutines.
-
Stop Tokens and Cancellation — Cooperative cancellation with
std::stop_token, and how Capy tasks observe it. -
Concurrent Composition — Running tasks concurrently with
when_allandwhen_any. -
Frame Allocators — How coroutine frames are allocated, and how to customize the allocator.
-
Lambda Coroutine Captures — A critical pitfall: lambda captures versus coroutine frame lifetime.