As a C++ lead, how do you decide whether and how your team adopts C++20 coroutines when the standard library supplies no task type or executor?
answer
- language feature, library gap
- who owns the task type
- C++26 std::execution::task
- lifetime rules as review policy
- toolchain and debugger support
basics
~20 sAdopt only with one owned or vetted task type, a scheduler story and lifetime rules the team enforces. C++20 gives the language and handles, C++23 only std::generator, and C++26's std::execution::task is missing from libc++ (LLVM 23) and libstdc++ (GCC 16.2).
solid answer
~50 sC++20 standardised the **language** — `co_await`, promise types, `std::coroutine_handle` — but no task type, scheduler or I/O. C++23 added only the synchronous `std::generator`. C++26 adds `std::execution` with `std::execution::task`, but at the pinned versions neither libc++ (LLVM 23) nor libstdc++ (GCC 16.2) ships it. So adopting coroutines means choosing **who owns the task type**: your networking or game framework's coroutine support, an established open-source library, or your own — the last being hard to get right (symmetric transfer, exceptions, cancellation, frame ownership). I would adopt where callback chains are the real pain — async I/O, multi-frame game logic — with one task type for the codebase, rules for where coroutines resume, a ban on reference parameters and capturing lambda coroutines, AddressSanitizer in CI, measured allocation cost, and a plan to migrate to `std::execution::task` once every target's library ships it.
go deeper
Know that C++20 provides coroutine keywords but no ready-made task type, so a project must pick one.
Explain what each standard added — C++20 handles, C++23 generator, C++26 std::execution::task — and what is still missing in today's libraries.
List the hard parts of a task type and the lifetime, allocation and resumption rules a codebase needs before coroutines spread.
Choose framework, library, in-house or wait; name the owner, the review rules and the migration path to the standard task type.
## The gap a lead has to fill C++20 coroutines are a **language** feature with a deliberately thin library. What the standard provides and what it leaves out: | Standard | Provides | Leaves to you | |---|---|---| | C++20 | `co_await`, `co_yield`, `co_return`; `<coroutine>`: `std::coroutine_handle`, `std::coroutine_traits`, `std::suspend_always`, `std::suspend_never`, `std::noop_coroutine()` | any task type, scheduler, thread pool, I/O, cancellation | | C++23 | `std::generator`, a synchronous lazy range | asynchronous tasks | | C++26 | `std::execution` (senders and receivers) with `std::execution::task`, a sender usable as a coroutine return type, plus `task_scheduler` and `run_loop` | adoption: library support is still arriving | At the pinned toolchains, libc++ (LLVM 23) lists both `std::execution` and the coroutine task type as not implemented, and libstdc++ (GCC 16.2) has no senders either — its `<execution>` header holds the C++17 parallel-algorithm policies. A team adopting coroutines today therefore picks a task type from outside the standard library. ## The options 1. **Your framework's coroutine support.** Networking and game frameworks often ship awaitables and task types integrated with their event loop. Lowest risk when the framework is already a dependency. 2. **An established open-source coroutine library.** Gives a tested task type, symmetric transfer and synchronisation primitives; adds a dependency and its upgrade cadence. 3. **An in-house task type.** Full control, but the hard parts are exactly the ones that fail quietly: frame ownership and `final_suspend`, symmetric transfer to avoid stack growth, exception propagation through `unhandled_exception`, cancellation, and which thread resumes. 4. **Wait for `std::execution::task`.** Right for a codebase whose toolchains will ship it soon; costs nothing now but delays the benefit. ## When coroutines are worth it - Asynchronous I/O written as callback chains or state machines, where `co_await` turns them back into straight-line code. - Game or simulation logic spanning many frames ("walk there, wait two seconds, then open the door"). - Lazy pipelines, where `std::generator` or a library generator replaces hand-written iterators. They are a poor trade for CPU-bound parallelism — coroutines add no threads — and for teams whose debuggers and profilers cannot yet show coroutine frames clearly; clang publishes a separate guide just for debugging coroutines. ## The policy to write down 1. **One task type** (and one generator type) for the codebase, owned by a named group; no ad-hoc promise types elsewhere. 2. **Resumption rules**: which thread or loop each awaiter resumes on, written into the awaiters rather than left to whoever completes the I/O. 3. **Lifetime rules**: no reference, view or pointer parameters on suspending coroutines without a documented owner; lambda coroutines take parameters instead of captures; member coroutines keep their object alive. 4. **Allocation**: measure frame allocations on hot paths; use pooled allocation through the promise where elision does not happen. 5. **Tooling**: AddressSanitizer runs over coroutine tests in CI; check that every compiler and debugger the team ships with handles coroutines. 6. **Exit plan**: wrap the chosen task type so that moving to `std::execution::task` later is a contained change. ## The tradeoffs a lead weighs - **Readability gain vs. new failure modes**: straight-line async code is easier to read, but dangling references across suspension are a new class of bug that compiles cleanly. - **Dependency vs. ownership**: a library brings tested primitives and a dependency; in-house brings control and a maintenance burden few engineers can carry. - **Now vs. the standard**: building on a non-standard task type today means a migration later; waiting means living with callbacks longer.
- What makes a home-grown task type hard to get right?It must own the frame and destroy it exactly once (matching `final_suspend` to the owner), resume the awaiting coroutine by returning its handle from `await_suspend` to avoid stack growth, carry exceptions from `unhandled_exception` to whoever awaits it, support cancellation, and define which thread continues after each `co_await`. Each of these fails quietly — leaks, use-after-free, stack overflow under load — rather than at compile time.
- Would you allow std::generator before the async task type is decided?Yes, where every target's standard library ships `<generator>`: it is standard, synchronous and self-contained, so it carries none of the scheduling questions. Where a library lacks it (libc++ in LLVM 23), allow one reviewed fallback generator type and plan to swap it out.
- C++20 includes a standard task type and thread pool for coroutines.
- Coroutines make code run in parallel on more threads.
- Each team should write its own promise types as needed.
- Coroutines have no runtime cost, so hot paths can use them freely.
- std::execution::task is available in every current standard library.
Assumes C++23 as current and C++26 as the draft that adds std::execution::task; library support was checked in LLVM 23 and GCC 16.2.