Coroutines: suspension as first‑class code
Suspension as a captured continuation
A coroutine can pause and later resume at the same point. The compiler transforms the function into a state machine of blocks, each ending with a stored continuation that captures the current locals. This continuation is a callable object representing the remaining work. The compiler allocates a heap‑based coroutine frame for locals that survive suspension.
The compiler can optimise away the frame or allocate it on the stack when possible. The generated state machine is invisible to the programmer, allowing the coroutine to be read as linear code.
The protocol: promise, awaiter, handle
The C++ coroutine framework defines three cooperating components.
The promise is a user‑defined type that lives inside the coroutine object. It holds the result value, any exception, and any additional state required for the algorithm. The compiler asks the promise for the object that will be returned to the caller (get_return_object). It also receives each value that is yielded or awaited (yield_value, await_transform).
The awaiter is a temporary object produced by the promise when a co_await expression appears. It tells the runtime whether the coroutine must suspend (await_ready), how to suspend (await_suspend), and how to retrieve the resumed value (await_resume). The awaiter can be a library‑provided type such as std::suspend_always or a custom type that performs I/O.
The handle (std::coroutine_handle) is a thin pointer to the suspended coroutine’s frame. It is the only object that can be stored, moved, or destroyed by user code. The handle provides resume, destroy, and done. End users normally manipulate only the handle returned by the promise. Library authors implement the promise and awaiter to expose a convenient API. The handle remains a low‑level plumbing artifact.
The promise creates the coroutine frame and returns a handle. The awaiter decides whether to suspend and, if so, stores the handle in the awaiting context. A later handle.resume() invokes the captured continuation. This separation lets library writers specialise behaviour (e.g., asynchronous I/O) without exposing low‑level mechanics.
A practical illustration is the standard std::generator. Its promise type stores the most recent yielded value and implements yield_value by saving that value and returning std::suspend_always. The awaiter in this case is trivial: every co_yield forces a suspension, and the handle is resumed by the range‑for iterator each time it requests the next element.
Libraries own the machinery
The C++ standard supplies a minimal protocol but does not expect most programmers to interact with it directly. Instead the standard library and third‑party libraries provide ready‑made abstractions such as std::generator, std::task, or std::async. These wrappers hide the promise, awaiter, and handle behind a clean interface. The guideline is to consume a coroutine by using a library type and to write a new promise type only when you need a custom behaviour that no existing library supplies. Because the library types are templates, they can be combined with other generic facilities. For example, a std::generator can be wrapped in std::ranges::view_interface to expose the full range adaptor API. This composability is a cornerstone of modern C++ design: write the low‑level plumbing once, then reuse it through higher‑level abstractions.
Additionally, the standard library provides utility awaiters such as std::suspend_never and std::suspend_always, as well as types derived from std::suspend_always, which integrate with the executor model introduced in later standards. Library authors can build higher‑level primitives, such as asynchronous file reads, by defining a custom promise that stores the I/O state and an awaiter that registers the operation with an event loop.
std::generator deep
std::generator<T> models a lazy sequence of values of type T. Inside the coroutine body the keyword co_yield places a value into the generator and suspends. The caller receives a range‑compatible object. Each iteration resumes the coroutine, evaluates the next co_yield, and returns the value. Because the generator satisfies the input‑range requirement it can be used with any range algorithm or view introduced in chapter 13.
The following example produces the Fibonacci numbers. The program asks the generator for the first eleven values and prints them on a single line. The test harness expects the string “55” to appear in the output, confirming that the eleventh value was produced.
#include <coroutine>
#include <exception>
#include <iostream>
#include <cstdint>
#include <optional>
// Minimal generator for uint64_t values.
template <typename T>
struct simple_generator {
struct promise_type {
std::optional<T> current;
auto get_return_object() { return simple_generator{handle_type::from_promise(*this)}; }
std::suspend_always initial_suspend() noexcept { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
std::suspend_always yield_value(T value) noexcept {
current = std::move(value);
return {};
}
void return_void() noexcept {}
void unhandled_exception() { std::abort(); }
};
using handle_type = std::coroutine_handle<promise_type>;
handle_type coro;
explicit simple_generator(handle_type h) : coro(h) {}
simple_generator(const simple_generator&) = delete;
simple_generator& operator=(const simple_generator&) = delete;
simple_generator(simple_generator&& other) noexcept : coro(other.coro) { other.coro = nullptr; }
simple_generator& operator=(simple_generator&& other) noexcept {
if (this != &other) {
if (coro) coro.destroy();
coro = other.coro;
other.coro = nullptr;
}
return *this;
}
~simple_generator() { if (coro) coro.destroy(); }
struct iterator {
handle_type coro;
bool done;
iterator(handle_type h, bool d) : coro(h), done(d) {}
iterator& operator++() { coro.resume(); done = coro.done(); return *this; }
const T& operator*() const {
if (!coro.promise().current.has_value()) std::abort();
return coro.promise().current.value();
}
bool operator==(std::default_sentinel_t) const { return done; }
};
iterator begin() { coro.resume(); return iterator{coro, coro.done()}; }
std::default_sentinel_t end() const { return {}; }
};
simple_generator<std::uint64_t> fibonacci() {
std::uint64_t a = 0, b = 1;
while (true) {
co_yield a;
auto next = a + b;
a = b;
b = next;
}
}
int main() {
std::size_t N = 11;
std::size_t i = 0;
for (auto v : fibonacci()) {
std::cout << v << (i + 1 == N ? '\n' : ' ');
if (++i >= N) break;
}
return 0;
}
The implementation uses an infinite loop that yields the current value before advancing the pair. The loop terminates in main after the required number of elements have been printed. This pattern demonstrates how a generator can represent an unbounded mathematical series while the consumer decides when to stop, a key advantage of lazy evaluation. It also shows that the generator does not allocate a container up‑front. The only allocation is the coroutine frame, which holds the two counters.
A hand‑rolled generator (book_demo)
The low‑level protocol can be assembled manually. The code below defines a minimal simple_generator<T> that follows the same pattern as std::generator. It declares a nested promise_type that stores the current yielded value in an std::optional<T>. The promise creates a simple_generator handle, supplies initial_suspend and final_suspend that always suspend, and implements yield_value by saving the value and returning std::suspend_always.
The outer simple_generator owns a std::coroutine_handle<promise_type>. It disables copy, enables move, and destroys the coroutine frame in its destructor. To make the object usable in a range‑for loop it provides an iterator type that resumes the coroutine on each increment, checks completion with coro.done(), and dereferences the stored value. The example coroutine numbers yields the first three natural numbers. When compiled and run the program prints “1 2 3”. The hand-rolled frame is the exact shape the compiler produces for std::generator, minus the safety checks and the range interface. It is worth reading once to make the abstraction concrete.
#include <coroutine>
#include <exception>
#include <iostream>
#include <optional>
// Minimal generator that yields values of type T.
// This is a book_demo: illustrative only, not for production use.
template <typename T>
struct simple_generator {
struct promise_type {
std::optional<T> current;
auto get_return_object() { return simple_generator{handle_type::from_promise(*this)}; }
std::suspend_always initial_suspend() noexcept { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
std::suspend_always yield_value(T value) noexcept {
current = std::move(value);
return {};
}
void return_void() noexcept {}
void unhandled_exception() { std::terminate(); }
};
using handle_type = std::coroutine_handle<promise_type>;
handle_type coro;
explicit simple_generator(handle_type h) : coro(h) {}
simple_generator(const simple_generator&) = delete;
simple_generator(simple_generator&& other) noexcept : coro(other.coro) { other.coro = nullptr; }
~simple_generator() { if (coro) coro.destroy(); }
// Iterator support for range‑for.
struct iterator {
handle_type coro;
bool done;
iterator(handle_type h, bool d) : coro(h), done(d) {}
iterator& operator++() {
coro.resume();
done = coro.done();
return *this;
}
const T& operator*() const { return *coro.promise().current; }
bool operator==(std::default_sentinel_t) const { return done; }
};
iterator begin() {
coro.resume();
return iterator{coro, coro.done()};
}
std::default_sentinel_t end() const { return {}; }
};
// Example: generate the first three natural numbers.
simple_generator<int> numbers() {
co_yield 1;
co_yield 2;
co_yield 3;
}
int main() {
for (int n : numbers()) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
This illustration is for reading only. Production code must prefer std::generator or a well‑tested library because the hand‑rolled version lacks many safety checks and does not participate in the standard library’s range ecosystem. Nevertheless, writing a generator by hand is an excellent learning exercise: it reveals how the promise, awaiter, and handle collaborate, and it shows where the compiler inserts the frame allocation and cleanup. Understanding this machinery equips you to diagnose compilation errors that arise when customizing coroutine behaviour, for example when integrating a custom I/O awaiter.
Coroutines and ranges
A std::generator satisfies the input‑range requirement and can be piped through any range adaptor from chapter 13. Because it yields values lazily, combining it with other lazy views incurs no intermediate storage. Each value is computed on demand. For example, std::views::take(5) | std::ranges::to<std::vector>() materialises the first five values, while std::views::filter(is_even) | std::views::transform(square) processes each element once. Materialisation occurs only at the boundary where ownership is required, matching the chapter 13 rule.
Another practical scenario is streaming data from a file or network socket. A coroutine can co_await an asynchronous read operation, co_yield each chunk as it arrives, and the surrounding range pipeline can std::ranges::copy the elements into a container or process them directly. The composition remains expression‑only, keeping the code concise and adhering to the Core Guidelines emphasis on clear intent.
co_await a value
The co_await operator can be applied to an ordinary value when an awaiter is provided that returns the value after suspension. The awaiter is what makes co_await compile, so the operator always pairs with a suspension mechanism. The snippet below shows a generator that awaits a helper coroutine compute before yielding the result. The helper returns int after a dummy delay. The awaiting generator resumes once the delay completes and yields the computed integer.
std::generator<int> delayed() {
int v = co_await compute(); // suspend until compute finishes
co_yield v;
}
In practice, co_await is most useful for integrating asynchronous I/O or heavy computation into a lazy pipeline. A generator can co_await a network read, produce each chunk as it arrives, and feed it directly into a range algorithm that processes the data incrementally. The awaiter for such a source registers the operation with an event loop and resumes the coroutine when the data is ready, so the generator yields a value only when one is actually available.
Try this
Write a std::generator<int> that lazily yields each integer record from a log stored in a std::string_view. The log consists of decimal numbers separated by newline characters. Parse each line, convert it with std::stoi, and co_yield the integer. Consume the generator with a range‑for loop and print each value. No solution is provided. Use the techniques described above.