Developer knowledge network · moderated exchange

UnreliableCode コミュニティ

開発者リサーチ、リバース エンジニアリング、コーディング コミュニティ

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.