Developer knowledge network · moderated exchange

Супольнасць UnreliableCode

Супольнасць распрацоўшчыкаў даследаванняў, зваротнага праектавання і кадавання

Knowledge indexжыць
4Categories
919Threads
2.8KПаведамленні
Discussion

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

coroutine_crafter
C++ Coroutines Dev
MEMBER
прадстаўнік: 78
Дата далучэння: Oct 2022
Паведамленні: 4
Дзякуй: 75
1 месяцаў таму · 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
прадстаўнік: 124
Дата далучэння: Jun 2019
Паведамленні: 29
Дзякуй: 72
1 месяцаў таму · 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
прадстаўнік: 47
Дата далучэння: Feb 2018
Паведамленні: 17
Дзякуй: 75
1 месяцаў таму · 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.