Awaitables and Awaiters
Get a detailed understanding of crucial abstraction in the coroutine framework known as 'awaitables' and 'awaiters'.
The three functions of a promise object: yield_value
, initial_suspend
, and final_suspend
return awaitables.
Awaitables
An Awaitable is something you can await on. The awaitable determines if the coroutine pauses or not.
Essentially, the compiler generates the three functions calls using the promise prom
and the co_await
operator.
Call | Compiler generated call |
---|---|
yield value |
co_await prom.yield_value(value) |
prom.initial_suspend() |
co_await prom.initial_suspend() |
prom.final_suspend() |
co_await prom.final_suspend() |
The co_await
operator needs an awaitable as argument. Awaitables have to implement the concept Awaitable.
The concept Awaitable
The concept Awaitable requires three functions.
Function | Description |
---|---|
await_ready |
Indicates if the result is ready. When it returns false , await_suspend is called. |
await_suspend |
Schedule the coroutine for resumption or destruction. |
await_resume |
Provides the result for the co_await exp expression. |
The C++20 standard already defines two basic awaitables: std::suspend_always
, and std::suspend_never
.
Get hands-on with 1400+ tech skills courses.