mp-coro main
Coroutine support tools
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | Related Functions | List of all members
task< T, Allocator > Class Template Reference

#include <mp-coro/task.h>

Detailed Description

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
TType of the value returned.
AllocatorHas no effect.
See also
https://lewissbaker.github.io/2020/05/11/understanding_symmetric_transfer
Example
task<> foo() {
co_return;
}
task<> bar() {
task<> f = foo();
co_await f;
}
Task that produces a value of type T: to get that value, simply await the task.
Definition: task.h:65

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.

+ Collaboration diagram for task< T, Allocator >:

Classes

struct  awaiter
 Awaiter type for awaiting the result of a task. More...
 
struct  promise_type
 Required promise type for coroutines returning a task. More...
 

Public Types

using value_type = T
 The type of the value produced by this task. More...
 

Public Member Functions

 task (task &&)=default
 Move constructor. More...
 
taskoperator= (task &&)=delete
 Move assignment not allowed. More...
 
awaiter_of< T > auto operator co_await () const &noexcept
 When awaited, sets the current coroutine as this task's continuation, and then resumes this task's coroutine. More...
 
awaiter_of< const T & > auto operator co_await () const &noexcept
 
awaiter_of< T && > auto operator co_await () const &&noexcept
 

Private Member Functions

 task (promise_type *promise)
 Private constructor used in promise_type::get_return_object. More...
 

Private Attributes

promise_ptr< promise_typepromise_
 An owning pointer to the promise object in the coroutine frame. More...
 

Related Functions

(Note that these are not member functions.)

template<awaitable A>
task< remove_rvalue_reference_t< await_result_t< A > > > make_task (A &&awaitable)
 

Member Typedef Documentation

◆ value_type

using value_type = T

The type of the value produced by this task.

Definition at line 68 of file task.h.

Constructor & Destructor Documentation

◆ task() [1/2]

task ( task< T, Allocator > &&  )
default

Move constructor.

◆ task() [2/2]

task ( promise_type promise)
inlineprivate

Private constructor used in promise_type::get_return_object.

Definition at line 170 of file task.h.

Member Function Documentation

◆ operator=()

task & operator= ( task< T, Allocator > &&  )
delete

Move assignment not allowed.

◆ operator co_await() [1/3]

awaiter_of< T > auto operator co_await ( ) const &
inlinenoexcept

When awaited, sets the current coroutine as this task's continuation, and then resumes this task's coroutine.

Definition at line 140 of file task.h.

◆ operator co_await() [2/3]

awaiter_of< const T & > auto operator co_await ( ) const &
inlinenoexcept

Definition at line 146 of file task.h.

◆ operator co_await() [3/3]

awaiter_of< T && > auto operator co_await ( ) const &&
inlinenoexcept

Definition at line 151 of file task.h.

Friends And Related Function Documentation

◆ make_task()

task< remove_rvalue_reference_t< await_result_t< A > > > make_task ( A &&  awaitable)
related

Definition at line 175 of file task.h.

Member Data Documentation

◆ promise_

promise_ptr<promise_type> promise_
private

An owning pointer to the promise object in the coroutine frame.

When the task is destructed, this will cause the coroutine frame to be destroyed automatically.

Definition at line 167 of file task.h.


The documentation for this class was generated from the following file: