Developer knowledge network ยท moderated exchange

Onbetrouwbare Code-gemeenschap

Ontwikkelaarsonderzoek, reverse engineering en coderingsgemeenschap

Knowledge indexLive
4Categories
919Threads
2.8KBerichten
Discussion

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

coroutine_crafter
C++ Coroutines Dev
MEMBER
Vertegenwoordiger: 78
Datum van deelname: Oct 2022
Berichten: 4
Bedankt: 75
1 maanden geleden ยท 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
Vertegenwoordiger: 124
Datum van deelname: Jun 2019
Berichten: 29
Bedankt: 72
1 maanden geleden ยท 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
Vertegenwoordiger: 47
Datum van deelname: Feb 2018
Berichten: 17
Bedankt: 75
1 maanden geleden ยท 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.