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.

Thread Safety

Distinct objects: Safe. Shared objects: Unsafe.

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

promise_type

Stores task<T>'s result and joins the I/O awaitable protocol via context_awaitable_promise_base.

Member Functions

Name

Description

task [constructor]

Constructors

~task [destructor]

Destroy the task and its coroutine frame if owned.

operator=

Assignment operators

await_ready

Report whether the awaited task is already complete.

await_resume

Return the task's result, rethrowing any captured exception.

await_suspend

Start the task with the awaiting coroutine's context.

handle

Return the coroutine handle.

release

Release ownership of the coroutine frame.

Data Members

Name

Description

h_

Handle to the owned coroutine frame.

Non-Member Functions

Name

Description

read

Read data from a stream until the buffer sequence is full.

read_at_least

Read at least a minimum number of bytes from a stream.

when_all

Execute results‐returning awaitables concurrently, inspecting error codes.

when_all

Execute a range of void results‐returning awaitables concurrently.

when_all

Execute a range of results‐returning awaitables concurrently.

when_any

Race results‐returning awaitables, selecting the first success.

when_any

Race a range of results‐returning awaitables (non‐void payloads).

when_any

Race a range of void results‐returning awaitables.

write

Write an entire buffer sequence to a stream.

write_at_least

Write at least a minimum number of bytes to a stream.

Return Value

The return value should not be discarded.

Template Parameters

Name

Description

T

The result type. Use task<> for task<void>.