Quick Start
Requirements
Compiler Support
CI tests these compilers. Older versions are untested.
-
GCC 13+
-
Clang 17+
-
Apple-Clang (macOS 15+)
-
MSVC 14.44+
-
MinGW Clang (MSYS2)
-
clang-cl
-
Clang on FreeBSD 14.3 and 15.0
Dependencies
None. Capy is self-contained and does not require Boost.
The optional Asio interop header, boost/capy/buffers/asio.hpp, needs Boost.Asio.
On macOS and FreeBSD, Clang needs -fexperimental-library for std::stop_token.
The CMake build adds this flag, but a hand-written compile line must add it too.
Minimal Example
Create a file hello_coro.cpp:
#include <boost/capy/task.hpp>
#include <boost/capy/ex/run_async.hpp>
#include <boost/capy/ex/thread_pool.hpp>
#include <iostream>
namespace capy = boost::capy;
// A coroutine that returns a value
capy::task<int> answer()
{
co_return 42;
}
// A coroutine that awaits another coroutine
capy::task<void> greet()
{
int n = co_await answer();
std::cout << "The answer is " << n << "\n";
}
int main()
{
capy::thread_pool pool(1);
// Start the coroutine on the pool's executor
capy::run_async(pool.get_executor())(greet());
// join() waits for outstanding work to complete; the pool
// destructor does not wait, and abandons any work still queued
pool.join();
}
Build and Run
Capy is not installed as a system package, so build it first. As the top-level project, the build also includes tests, examples, and benchmarks by default. These options turn them off for a faster first build:
cmake -B build -DBOOST_CAPY_BUILD_TESTS=OFF \
-DBOOST_CAPY_BUILD_EXAMPLES=OFF -DBOOST_CAPY_BUILD_BENCH=OFF
cmake --build build
Replace both paths below with where you built Capy:
g++ -std=c++20 -I/path/to/capy/include -o hello_coro hello_coro.cpp \
/path/to/capy/build/libboost_capy.a -pthread
If your program is built with CMake, add Capy with add_subdirectory or FetchContent, then link against Boost::capy.
|
Then run the compiled program:
./hello_coro
Expected output:
The answer is 42
What Just Happened?
-
answer()creates a suspended coroutine that returns 42 -
greet()creates a suspended coroutine that awaitsanswer() -
run_async(pool.get_executor())(greet())startsgreet()on the pool’s executor -
greet()runs until it hitsco_await answer() -
answer()runs and returns 42 -
greet()resumes with the result and prints it -
greet()completes,pool.join()returns, andmain()exits
Both coroutines ran on the same executor because affinity propagated
automatically through the co_await.
Handling Results
To receive a task’s result outside a coroutine, provide a completion handler.
In this snippet and the next, executor is pool.get_executor(), as in the minimal example:
capy::run_async(executor, [](int result) {
std::cout << "Got: " << result << "\n";
})(answer());
Handling Errors
Exceptions propagate through coroutine chains. To handle them at the top level:
capy::run_async(executor,
[](int result) {
std::cout << "Success: " << result << "\n";
},
[](std::exception_ptr ep) {
try {
if (ep) std::rethrow_exception(ep);
} catch (std::exception const& e) {
std::cerr << "Error: " << e.what() << "\n";
}
}
)(might_fail());
Error Codes Instead of Exceptions
I/O operations do not throw for expected failures, such as reaching the end of a stream.
They report the failure as a value instead: a results, holding an error code followed by the payload.
A task of your own can do the same, and the caller unpacks the error code first:
capy::task<capy::results<int>> parse_port(std::string_view s)
{
int port = 0;
auto [end, err] = std::from_chars(s.data(), s.data() + s.size(), port);
if(err != std::errc() || end != s.data() + s.size())
co_return {std::make_error_code(std::errc::invalid_argument), 0};
co_return {std::error_code(), port};
}
capy::task<> connect_to_server()
{
auto [ec, port] = co_await parse_port("8080"); // results<int>: error code first, then the value
if(ec)
{
std::cerr << "Bad port: " << ec.message() << "\n";
co_return;
}
std::cout << "Connecting on port " << port << "\n";
}
See The results Type.
Next Steps
-
Introduction To C++20 Coroutines: if you need to refresh the C++ coroutines basics
-
The task Type: learn how lazy tasks work
-
The results Type: how operations report errors without throwing
-
Starting Coroutines: understand
run_asyncin detail -
Executors and Execution Contexts: control where coroutines execute