Coroutine Concepts co_await co_yield co_return
Learn the three coroutine keywords and how the compiler transforms them.
Coroutine Concepts co_await co_yield co_return is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What are Coroutines?
A coroutine is a function that can suspend and later resume. Useful for asynchronous code, generators, and cooperative multitasking.
The Three Keywords
C++20 introduces three new keywords for coroutines:
co_await— suspend until an awaitable completesco_yield— suspend and yield a valueco_return— finish the coroutine, optionally with a value
What Makes a Function a Coroutine
A function becomes a coroutine if its body uses any of the three keywords. The return type must satisfy specific compiler-required traits.
Return Type Requirements
The return type must have a nested promise_type. The compiler uses it to manage the coroutine s state. The standard library does not provide one — you write or borrow it.
Generators (Conceptual)
A generator coroutine yields values lazily.
Generator<int> counting() {
int n = 0;
while (true) {
co_yield n++;
}
}
auto g = counting();
std::cout << g.next(); // 0
std::cout << g.next(); // 1Async Tasks (Conceptual)
An async task suspends on I/O and resumes when complete.
Task<int> fetch() {
auto response = co_await http_get("/api/value");
co_return parse(response);
}Coroutines Are Stackless
C++ coroutines do not have a separate stack. State is stored in a compiler-generated coroutine frame, typically on the heap. Cheaper than threads, more flexible than stackful coroutines.
Cooperative Scheduling
Coroutines do not preempt — they yield voluntarily. This makes them deterministic and avoids most concurrency hazards.
Where C++ Coroutines Shine
Strengths:
- Asynchronous I/O without callback hell
- Lazy sequence generation
- State machines as straight-line code
- Parallel pipelines
Where They Need Work
The C++20 coroutine TS is a language feature only. The standard library does not yet ship std::generator (C++23 added one) or std::task. You need a library — cppcoro, asio, or a custom implementation.
Mental Model
Think of a coroutine as a function that remembers where it left off. Each suspension point is a checkpoint; calling resume continues from there.
Coroutines vs Threads
Threads are preemptive and parallel; coroutines are cooperative and (typically) single-threaded. Coroutines are vastly cheaper to create — millions can coexist. They complement, not replace, threads.
Quick Check
Which keyword would you use to yield a value from a generator coroutine?
Recap
C++20 coroutines use co_await, co_yield, and co_return. They are stackless and cooperative. The language provides the mechanism; libraries provide the types (generators, tasks). Powerful for async I/O and lazy sequences.
Frequently asked questions
Is the “Coroutine Concepts co_await co_yield co_return” lesson free?
Yes — the full text of “Coroutine Concepts co_await co_yield co_return” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Coroutine Concepts co_await co_yield co_return”?
Learn the three coroutine keywords and how the compiler transforms them. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Coroutine Concepts co_await co_yield co_return” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Coroutine Concepts co_await co_yield co_return
- Implementing a Simple Generator
- Async Tasks and Awaiter Types
- Coroutine Frame Allocation