The task Type
Overview
Key characteristics:
-
Lazy execution — The coroutine does not start until awaited
-
Symmetric transfer — Efficient resumption without stack accumulation
-
Executor inheritance — Inherits the caller’s executor unless explicitly bound
-
Stop token propagation — Forward-propagates cancellation signals
-
HALO support — Enables Heap Allocation eLision Optimization when possible
Declaring task Coroutines
Any function that returns task<T> and contains coroutine keywords (co_await, co_return) is a task coroutine:
#include <boost/capy.hpp>
namespace capy = boost::capy;
capy::task<int> compute_value()
{
co_return 42;
}
capy::task<std::string> fetch_greeting()
{
co_return "Hello, Capy!";
}
capy::task<> do_nothing() // task<void>
{
co_return;
}
The syntax task<> is equivalent to task<void>.
Returning Values with co_return
Use co_return to complete the coroutine and provide its result:
capy::task<int> add(int a, int b)
{
int result = a + b;
co_return result; // Completes with value
}
capy::task<> log_message(std::string msg)
{
std::cout << msg << "\n";
co_return; // Completes without value
}
For task<void>, you can either use co_return; explicitly or let execution fall off the end of the function body.
Reporting Errors: results
I/O operations report an expected failure as a value rather than an exception, and a coroutine that reports errors this way returns task<results<Ts…>>.
The results Type explains how to consume and produce a results, which the stream concepts and the concurrent combinators all use.
Running a Task
A task is lazy, so declaring one does not run it. To run a task to completion from ordinary (non-coroutine) code, pass it to run_async with an executor and a completion handler. The task runs on the executor, and the completion handler receives its result:
// You have a task; run it on an executor and observe its result.
capy::thread_pool pool(1);
auto ex = pool.get_executor();
int total = 0;
capy::run_async(ex, [&](int result) {
std::cout << "Result: " << result << "\n"; // prints 5
total = result;
})(add(2, 3));
pool.join(); // wait for the pooled task to finish
Here add(2, 3) runs on a thread_pool executor, and the completion handler receives the result, 5. The call to pool.join() waits for the pooled work to finish before the result is read.
Starting Coroutines covers run_async in depth, and Executors and Execution Contexts covers thread pools.
Awaiting Other Tasks
Tasks can await other tasks using co_await. This is the primary mechanism for composing asynchronous operations:
capy::task<int> step_one()
{
co_return 10;
}
capy::task<int> step_two(int x)
{
co_return x * 2;
}
capy::task<int> full_operation()
{
int a = co_await step_one(); // Suspends until step_one completes
int b = co_await step_two(a); // Suspends until step_two completes
co_return b + 5; // Final result: 25
}
When you co_await a task:
-
The current coroutine suspends
-
The awaited task starts executing
-
When the awaited task completes, the current coroutine resumes
-
The
co_awaitexpression evaluates to the awaited task’s result
Lazy Execution
A critical property of task<T> is lazy execution: creating a task does not start its execution.
capy::task<int> compute()
{
std::cout << "Computing...\n"; // Not printed until awaited
co_return 42;
}
capy::task<> example()
{
auto t = compute(); // Task created, but "Computing..." NOT printed yet
std::cout << "Task created\n";
int result = co_await std::move(t); // NOW "Computing..." is printed
std::cout << "Result: " << result << "\n";
}
Output:
Task created
Computing...
Result: 42
Lazy execution enables efficient composition—tasks that are never awaited never run, consuming no resources beyond their initial allocation.
Symmetric Transfer
When a task completes, control transfers directly to its continuation (the coroutine that awaited it) using symmetric transfer. This avoids stack accumulation even with deep chains of coroutine calls.
capy::task<> a() { co_await b(); }
capy::task<> b() { co_await c(); }
capy::task<> c() { co_return; }
Without symmetric transfer, each co_await would add a stack frame, potentially causing stack overflow with deep nesting. With symmetric transfer, c returning to b returning to a uses constant stack space regardless of depth.
The task’s final-suspend awaiter implements this: its await_suspend returns a coroutine handle rather than void:
// Inside task's final_suspend awaiter
std::coroutine_handle<> await_suspend(std::coroutine_handle<>) const noexcept
{
return continuation_; // Transfer directly to continuation
}
Move Semantics
Tasks are move-only. Copying a task would create aliasing problems where multiple handles reference the same coroutine frame.
capy::task<int> compute();
capy::task<> example()
{
auto t1 = compute();
auto t2 = std::move(t1); // OK: ownership transferred, t1 is now empty
// auto t3 = t2; // Error: task is not copyable
int result = co_await std::move(t2);
}
After moving, the source task becomes empty and must not be awaited. A task must not be awaited twice either. The second await would resume a coroutine that has already finished.
Exception Propagation
Exceptions thrown inside a task are captured and rethrown when the task is awaited:
capy::task<int> might_fail(bool should_fail)
{
if (should_fail)
throw std::runtime_error("Operation failed");
co_return 42;
}
capy::task<> example()
{
try
{
int result = co_await might_fail(true);
}
catch (std::runtime_error const& e)
{
std::cout << "Caught: " << e.what() << "\n";
}
}
The exception is stored in the promise when it occurs and rethrown in await_resume when the calling coroutine resumes.