The Workflow
Discover a dynamic aspect of coroutines called workflow.
We'll cover the following...
The compiler transforms your coroutine and runs two workflows: the outer promise workflow and the inner awaiter workflow.
The promise workflow
When you use co_yield
, co_await
, or co_return
in a function, the function becomes a coroutine, and the compiler transforms its body to something equivalent to the following lines.
Press + to interact
{Promise prom;co_await prom.initial_suspend();try {<function body having co_return, co_yield, or co_wait>}catch (...) {prom.unhandled_exception();}FinalSuspend:co_await prom.final_suspend();}
The compiler automatically runs the transformed code using the functions of the promise object. In short, I call this workflow the promise workflow. Here are the main steps of this workflow:
-
Coroutine begins execution
- allocates the coroutine frame if necessary
- copies all function parameters to the coroutine frame
- Creates the prom object
prom
(line 2) - Calls
prom.get_return_object()
to create the coroutine handle, and keeps it in a local variable. The result of the call will be returned to the caller when the coroutine first suspends. - Calls
prom.initial_suspend()
and