Leanpub Header

Skip to main content

Production C++23/26 Coroutines

Engineering Zero-Allocation Async Runtimes with std::execution and Structured Concurrency

Production C++23/26 Coroutines
This book is 100% completeLast updated on 2026-12-01

Coroutines look synchronous, and that is exactly why they fail in production. This book builds a complete async runtime in modern C++: tasks, schedulers, frame allocators, structured concurrency, cancellation, and a bridge to C++26 std::execution. Every chapter starts with a real failure and ends with a measured cost, down to a tested zero allocations per request.

Minimum price

$29.00

$39.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

Your coroutines pass every test. Then production load makes the I/O genuinely asynchronous.

A const std::string& parameter dangles. A stack overflows only when the cache is warm. A lost wakeup stalls a pool for fifty milliseconds. A fire-and-forget coroutine crashes the service at shutdown, once every twenty deploys. None of these are compile errors, and none show up in a thread dump, because a suspended coroutine is not on any stack.

This book is about those failures: why they happen, how to reproduce them on demand, and how to build a runtime where they cannot.

What you will build

Over fourteen chapters you build one runtime, piece by piece, and every piece is tested under AddressSanitizer and ThreadSanitizer:

  • A lazy task type with symmetric transfer, and a measured explanation of why chains of synchronous completions overflow the stack
  • sync_wait, a single-threaded run loop, a thread pool, and a work-stealing scheduler, with the sleep protocol that avoids lost wakeups
  • Frame allocators (per-worker pools and request arenas) that bring steady-state heap allocations to a proven zero
  • when_all, when_any, async scopes, and fail-fast error propagation that make fire-and-forget impossible
  • Cooperative cancellation with std::stop_token, deadlines, race-free cancellation, and shielded async cleanup
  • Async mutexes, semaphores, channels, and a timer wheel integrated into the event loop
  • Adapters that connect all of it to C++26 std::execution, with a phased migration plan for an existing runtime

What makes it different

  • Every chapter opens with a production failure and closes with a ledger. The Allocation and Latency Ledger records heap allocations per operation, suspend-and-resume cost, and how correctness was verified. Read in sequence, the ledgers trace the runtime from three heap allocations for three trivial calls to a measured zero.
  • Real sanitizer output, not hand-waving. You see the actual AddressSanitizer and ThreadSanitizer reports for each bug, with the lines that identify it, so you can recognize them in your own logs.
  • Numbers as honest ranges. Costs are given as typical ranges, never as invented benchmark results, and "zero-allocation" is always shown with a counter, never asserted.
  • Opinionated where the field is divided. Lazy over eager tasks. Parameters by value. No detached coroutines. Hop to change the kind of thread, never just the thread. You get a position and the reasoning behind it.
  • Debugging and testing built in. Async stack traces, a live-frame registry, seeded simulation of coroutine interleavings with virtual time, sync-point tests that force a lost wakeup on every run, and tracing that survives thread hops.
  • GCC, Clang, and MSVC, side by side. Elision, tail calls, frame layout, diagnostics, and sanitizer support differ between compilers, and the book says exactly how.

Inside the book

  • Part I, Foundations: frames, promises, awaiters, handles, and the real cost model
  • Part II, Generators: std::generator, ranges composition, elements_of, and where a hand-written iterator still wins
  • Part III, Tasks and Runtimes: task types, symmetric transfer, schedulers, zero-allocation frames
  • Part IV, Structured Concurrency: scopes, when_all, cancellation, timeouts, lifetimes across suspension
  • Part V, std::execution: senders and receivers, custom senders, coroutines and senders in both directions, migration
  • Part VI, Production: wrapping C libraries, timers, async primitives, interop with threads, debugging, profiling, library API design
  • Appendices: customization points, std::execution algorithm reference, compiler and library support matrix, and a coroutine code review checklist
Who this book is for
  • Engineers who will run coroutine code in production: request services, market data and trading infrastructure, storage engines, database clients, game job systems
  • Library authors designing coroutine or sender-based APIs
  • Teams migrating callback or future-based code, or a custom runtime, toward std::execution

You should be comfortable with templates, move semantics, RAII, the memory model, and threads. This is not an introduction to what coroutines are.

What you will be able to do
  • Predict which thread resumes a coroutine, and who owns its frame, from the code alone
  • Find and fix dangling parameters, closure traps, double destroys, lost wakeups, and cancellation races
  • Prove a request path allocates nothing, and keep it that way in CI
  • Choose between your own runtime, stdexec, and the standard task, and migrate between them in safe phases
  • Review coroutine code against a concrete checklist
Requirements

GCC 13+, Clang 17+, or Visual Studio 2022; CMake 3.25+; NVIDIA stdexec for Part V onward. Linux is recommended for ThreadSanitizer and perf.

Bundle

Bundles that include this book

Author

About the Author

Weston Ashgrove

Weston Ashgrove is a C++ systems engineer who builds software where being wrong or slow has a cost. He came up through quantitative trading, building order books, matching logic and the low-latency infrastructure underneath them and now brings the same discipline to performance engineering, concurrency, storage, networking, GPUs, and AI inference.

His rule for performance-critical code is simple: nothing is true until it has been profiled, fuzzed, and replayed. Every technique in his books comes with the measurement that proves it, the trade-off it costs, and the failure it prevents. He writes for working engineers who ship and debug production C++, and he leaves out anything that only works on a slide.

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub