Developer knowledge network · moderated exchange

UnreliableCode-Community

Community für Entwicklerforschung, Reverse Engineering und Codierung

Knowledge indexLive
4Categories
919Threads
2.8KBeiträge
Discussion

How C++20 Coroutines work: breaking down co_await, promise_type, and coroutine_handle [StackOverflow Architecture Guide]

coroutine_crafter
C++ Coroutines Dev
MEMBER
Vertreter: 78
Beitrittsdatum: Oct 2022
Beiträge: 4
Danke: 75
Vor 1 Monaten · Jul 5, 2026 12:58 PM
#1

Unlike stackful coroutines (like Go goroutines or Lua coroutines), C++20 coroutines are completely stackless.

When a function contains co_await or co_yield:

  1. The compiler transforms the local variables into a heap-allocated coroutine frame object.
  2. Generates an internal state machine.
  3. The promise_type customizes return values via get_return_object(), initial_suspend(), and return_value().
  4. Calling handle.resume() continues execution at the exact yield suspension point without allocating an OS thread stack.
modern_cpp_artisan
C++ Template Wizard
MEMBER
Vertreter: 124
Beitrittsdatum: Jun 2019
Beiträge: 29
Danke: 72
Vor 1 Monaten · Jul 5, 2026 7:21 PM
#2

Stackless coroutines have virtually zero memory footprint (typically 32 to 96 bytes per frame), enabling millions of concurrent async tasks.

cpp_concurrency_guru
C++ Standards Expert
MEMBER
Vertreter: 47
Beitrittsdatum: Feb 2018
Beiträge: 17
Danke: 75
Vor 1 Monaten · Jul 6, 2026 8:39 AM
#3

The hardest part for beginners is writing custom awaitables. Once you implement a reusable Task<T>, it becomes as ergonomic as C# async/await.