Understanding C++20 Coroutines
Phil Nash
09:00-18:00, Saturday, 12th April 2025
Coroutines, introduced in C++20, offer a powerful new method for managing the flow of execution in your code. Whether you need to alternate between two or more streams of code in a linear fashion or handle fully asynchronous tasks, Coroutines present a fresh C++ approach to these challenges. They provide the low-level control necessary to achieve optimal performance (with some caveats) while also simplifying the code you write. Though they address a complex set of problems, Coroutines can be tamed! We’ll look at how to get started with minimal knowledge and effort, and then scale up to a solid understanding of how it all hangs together under the hood — and we will use that knowledge to good effect.
Outline
- A compiler-driven approach to understanding coroutines by writing them
- Our first MVP coroutine
- Resuming and destroying coroutines
- Getting data out of coroutines with
- with co_return
- with co_yield
- Generators and C++26's std::generate
- Awaiters
- Trivially awaitable types
- Writing an awaiter (compiler-driven)
- Passing values back into the coroutine
- A sleeping awaiter
- Resuming on a different thread
- await_transform
- coroutine_traits
- Custom allocators and the HALO optimisation
- RAII helper for managing handles (and following the rule of zero)
- Use cases and larger scale worked example
Phil Nash
Phil is the original author of the C++ test framework, Catch2 and is an independent consultant and trainer, specialising in TDD and Modern C++.
Formerly Developer Advocate at Sonar and JetBrains he has had a career that spans finance, mobile and software security.
He's also a member of the ISO C++ standards committee, organiser of C++ London and C++ on Sea, as well as co-host and producer of CppCast.