Example Programs
These examples demonstrate how the pieces you have learned—tasks, buffers, streams, cancellation, composition—fit together in practice.
Every example is self-contained and compiles as a standalone program.
What This Section Covers
-
Hello Task — The minimal Capy program: a task that prints a message.
-
Producer-Consumer — Two tasks communicating via an async event, with strand serialization.
-
Buffer Composition — Composing buffer sequences without allocation for scatter/gather I/O.
-
Mock Stream Testing — Unit testing protocol code with mock streams and error injection.
-
Type-Erased Echo — An echo server demonstrating the compilation firewall pattern.
-
Timeout with Cancellation — Racing a slow operation against a deadline, and cancelling it with a stop token.
-
Parallel Fetch — Running multiple operations concurrently with
when_all. -
Strand Serialization — Protecting shared state with a strand instead of a mutex.
-
Async Mutex — Fair FIFO coroutine locking with
async_mutex. -
Parallel Tasks — Distributing CPU-bound work across a thread pool and collecting results.
-
Custom Executor — Implementing the
Executorconcept with a single-threaded run loop. -
Bridging a P2300 Sender — Awaiting a
std::execution(P2300) sender from a Capy coroutine. -
Calling Asio from a Capy Coroutine — Using Boost.Asio operations directly through a
use_capycompletion token. -
GUI Integration — Running a Capy coroutine on a GUI event loop, resuming on the GUI thread to update widgets.