The Framework

Understand the relation between promise object, coroutine handle, and coroutine frame.

The framework for implementing coroutines consists of more than 2020 functions, some of which you must implement and some of which you may overwrite. Therefore, you can tailor the coroutine to your needs.

A coroutine is associated with three parts: the promise object, the coroutine handle, and the coroutine frame. The client gets the coroutine handle to interact with the promise object, which keeps its state in the coroutine frame.

Promise object

The promise object is manipulated from inside the coroutine. It delivers its result or exception via the promise object.

The promise object must support the following interface.

Member function Description
Default constructor A promise must be default constructible.
initial_suspend() Determines if the coroutine suspends before it runs.
final_suspend() Determines if the coroutine suspends before it ends.
unhandled_exception() Called when an exception happens.
get_return_object() Returns the coroutine object (resumable object).
return_value(val) Is invoked by co_return val.
return_void() Is invoked by co_return.
yield_value(val) Is invoked by co_yield val.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy