Developer knowledge network · moderated exchange

Zajednica UnreliableCode

Zajednica za istraživanje, obrnuti inženjering i programiranje programera

Knowledge indexŽivjeti
4Categories
919Threads
2.8KPostovi
Discussion

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

coroutine_crafter
C++ Coroutines Dev
MEMBER
Rep: 78
Datum pridruživanja: Oct 2022
Postovi: 4
Hvala: 75
prije 1 mjeseci · 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
Rep: 124
Datum pridruživanja: Jun 2019
Postovi: 29
Hvala: 72
prije 1 mjeseci · 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
Rep: 47
Datum pridruživanja: Feb 2018
Postovi: 17
Hvala: 75
prije 1 mjeseci · 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.