04 May 2025
Principia coroutines
Index of articles exploring C++ coroutines
What would it mean to create your own coroutine framework?
This series of articles/presentations is reviving an attempt to explore C++ coroutines that I worked on and off from 2023 onwards. It is a learning journey that covers interesting, but less common parts of C++.
Ivan: “This is not the C++ I’m used to see”
There are multiple goals. There is the indirect goal of generally learning about concurrency, about C++, but also specifically answering the question “what would it take to write a library that can do concurrency in C++, potentially involving coroutines”?
WORK IN PROGRESS
Presentations
- Problem domain
- Mechanics
- Synthetic coroutine
- Generator
- Lazy task
- Intrusive heap
- Concurency threading models
- Callback
- Stop source, token, callback
- Structured concurency
Bibliography
- spec N4775: relatively short, readable, but background info required
- Lewis Baker’s posts: long, not up to date, but really good background info required to understand coroutines
- Nathaniel J. Smith: Notes on structured concurrency, or: Go statement considered harmful
- C++ Coroutines at Scale - Implementation Choices at Google - Aaron Jacobs - C++Now 2024
- C++ Coroutines and Structured Concurrency in Practice - Dmitry Prokoptsev - C++Now 2024
- Structured Concurrency: Writing Safer Concurrent Code with Coroutines… - Lewis Baker - CppCon 2019
- CppCon 2018: G. Nishanov “Nano-coroutines to the Rescue! (Using Coroutines TS, of Course)
- Structured Networking in C++ - Dietmar Kühl - CppCon 2022
- P2300R10 std::execution
- P2175R0: Composable cancellation for sender-based async operations
- P3552R1 Add a Coroutine Task Type
- Introduction to Wait-free Algorithms in C++ Programming - Daniel Anderson - CppCon 2024
TODO - topics to cover
- Cancellation
- hiding cancellation from function signature (e.g. see
co_await get_cancellation_token()
- hiding cancellation from function signature (e.g. see
- Trampoline
- Timers
- Threading synchronization in await_suspend
- none (no cancellation)
- mutex
- atomic bool
- C++ coroutine weak points
- weak allocation control: have allocator, but allocation size is runtime
info
- no option to allocate on stack e.g. for a know type can control allocation on stack or heap (std::unique_ptr)
- resume and destroy mechanisms are indirect calls: call a function that fetches a function pointer that makes a switch
- a large vocabulary of terms: learning curve obstacle
- weak allocation control: have allocator, but allocation size is runtime
info
- senders receivers
- compare example with read size then data (senders vs. coroutine)
- design by committee (poorly explained, e.g. alternatives not explained)
- customization points overload
- Windows Thread Pool