boost::capy::task
Defers a coroutine body until awaited, then runs it inline on the caller's thread.
Synopsis
Declared in <boost/capy/task.hpp>
template<typename T = void>
struct [[nodiscard]] task;
Description
Use task<T> as the return type for coroutines that perform I/O and return a value of type T. The coroutine body does not start executing until the task is awaited, enabling efficient composition without unnecessary eager execution.
The task participates in the I/O awaitable protocol: when awaited, it receives the caller's executor and stop token, propagating them to nested co_await expressions. This enables cancellation and proper completion dispatch across executor boundaries.
Await‐effects
Let t be a task<T>. co_await t always suspends the awaiting coroutine, then transfers control directly into the task's coroutine body on the current thread; no executor operation is posted. The task records the caller's environment (executor, stop token, and frame allocator) by pointer rather than copying it. It propagates that environment to every co_await inside the body.
The body runs until it returns or exits via an exception. Control then transfers directly back to the awaiting coroutine, again without an executor operation.
task never inspects the stop token; it only propagates it. A task body observes a stop request through the results of the operations it awaits, or by reading the token itself.
Await‐returns
The value the body passed to co_return, moved out of the task, or nothing when T is void.
If the body exits via an unhandled exception, that exception is rethrown instead.
Await‐postcondition
The task's coroutine has run to completion and is suspended at its final suspend point. The task still owns the frame, but not the result: the await moves it out, so a task must not be awaited twice.
Example
capy::task<int> compute_value( capy::any_read_stream& stream, capy::mutable_buffer buf )
{
auto [ec, n] = co_await stream.read_some( buf );
if( ec )
co_return 0;
co_return static_cast<int>( n );
}
capy::task<> run_session( capy::any_read_stream& stream, capy::mutable_buffer buf )
{
int result = co_await compute_value( stream, buf );
}
Types
Name |
Description |
Stores |
Member Functions
Name |
Description |
|
Constructors |
|
Destroy the task and its coroutine frame if owned. |
Assignment operators |
|
Report whether the awaited task is already complete. |
|
Return the task's result, rethrowing any captured exception. |
|
Start the task with the awaiting coroutine's context. |
|
Return the coroutine handle. |
|
Release ownership of the coroutine frame. |
Non-Member Functions
Name |
Description |
Read data from a stream until the buffer sequence is full. |
|
Read at least a minimum number of bytes from a stream. |
|
Execute |
|
Execute a range of void |
|
Execute a range of |
|
Race |
|
Race a range of |
|
Race a range of void |
|
Write an entire buffer sequence to a stream. |
|
Write at least a minimum number of bytes to a stream. |
Return Value
|
The return value should not be discarded. |
See Also
Created with MrDocs