template<task_value_type T = void, typename Allocator = void>
class mp_coro::task< T, Allocator >
Task that produces a value of type T
: to get that value, simply await the task.
Tasks are lazy: they always suspend initially. Each task has an optional continuation (i.e. the coroutine that is waiting for the result of this task) that is resumed at the final suspend point.
- Template Parameters
-
T | Type of the value returned. |
Allocator | Has no effect. |
- See also
- https://lewissbaker.github.io/2020/05/11/understanding_symmetric_transfer
- Example
co_return;
}
co_await f;
}
Task that produces a value of type T: to get that value, simply await the task.
flowchart
start(( ))
subgraph bar
bar_coro_alloc["coroutine frame allocation\npromise_type promise {};
\npromise.get_return_object()
"]
bar_coro_alloc --> call_foo["task<> f = foo();
"]
await_f["co_await f;
"]
await_f --> op_co_await["f.operator co_await()
"]
awaiter_promise["task::awaiter a {f.promise}
"]
op_co_await --> awaiter_promise
await_rdy["a.await_ready()
"]
awaiter_promise --> await_rdy
await_rdy --> if_await_rdy{{"f.done()
"}}
if_await_rdy -->|false| await_sus["a.await_suspend(bar)
"]
if_await_rdy -->|true| await_rsm["a.await_resume()
"]
await_sus --> await_sus_body["f.promise.continuation = bar\nreturn foo
"]
await_rsm --> bar_ret_void["promise.return_void()
"]
bar_ret_void --> destroy_f["f.~task()
"]
destroy_f --> destroy_foo["foo.destroy()
"]
destroy_foo --> bar_final_sus["promise.final_suspend()
"]
end
subgraph foo
coro_alloc["coroutine frame allocation\npromise_type promise {};
\npromise.get_return_object()
"]
coro_alloc --> init_sus["co_await promise.initial_suspend()
"]
co_ret["co_return;
"]
co_ret --> ret_void["promise.return_void()
"]
ret_void --> final_sus["promise.final_suspend()
"]
final_sus --> final_sus_a["task::promise_type::final_awaiter a {}
"]
final_sus_a --> final_await_sus["a.await_suspend(foo)
"]
final_await_sus --> final_await_sus_body["return promise.continuation
"]
end
end_(( ))
start --> bar_coro_alloc
call_foo -->|Function call| coro_alloc
init_sus -->|Suspend| await_f
await_sus_body -->|Symmetric Control Transfer| co_ret
final_await_sus_body -->|Symmetric Control Transfer| await_rsm
bar_final_sus --> end_
- Examples
- async_read_file.cpp, run_async.cpp, simple_async_tasks.cpp, simple_tasks.cpp, and sleep_for.cpp.
Definition at line 65 of file task.h.