Coroutines (C++20 Overview)
Prerequisites: Mutexes & condition variables
A coroutine is a function that can suspend and later resume
exactly where it left off — C++20's co_await/co_yield/co_return
keywords opt a function into this, but (unlike some languages) the
standard library ships almost no ready-made coroutine types of its
own; using them well typically means either a library (like
cppcoro) or writing your own supporting types.
// Conceptual shape only — a real generator needs a supporting
// "promise type" beyond the scope of this overview lesson.
Generator<int> countUpTo(int n) {
for (int i = 0; i < n; i++) {
co_yield i; // suspend here, producing i, resuming on next request
}
}
This lesson deliberately stays at the conceptual level — coroutines are one of C++20's most powerful but least beginner-friendly features, and are worth a dedicated deep dive once the rest of modern C++ feels solid.