Parallel Fetch
Running multiple operations concurrently with when_all.
What This Example Shows
-
Using
when_allto run tasks in parallel -
Structured bindings for results
-
Error propagation in concurrent tasks
Source Code
#include <boost/capy.hpp>
#include <iostream>
#include <latch>
#include <string>
#include <vector>
namespace capy = boost::capy;
// Simulated async operations
capy::task<int> fetch_user_id(std::string username)
{
std::cout << "Fetching user ID for: " << username << "\n";
// In real code: co_await http_get("/users/" + username);
co_return static_cast<int>(username.length()) * 100; // Fake ID
}
capy::task<std::string> fetch_user_name(int id)
{
std::cout << "Fetching name for user ID: " << id << "\n";
co_return "User" + std::to_string(id);
}
capy::task<int> fetch_order_count(int user_id)
{
std::cout << "Fetching order count for user: " << user_id << "\n";
co_return user_id / 10; // Fake count
}
capy::task<double> fetch_account_balance(int user_id)
{
std::cout << "Fetching balance for user: " << user_id << "\n";
co_return user_id * 1.5; // Fake balance
}
// Fetch all user data in parallel
capy::task<> fetch_user_dashboard(std::string username)
{
std::cout << "\n=== Fetching dashboard for: " << username << " ===\n";
// First, get the user ID (needed for other queries)
int user_id = co_await fetch_user_id(username);
std::cout << "Got user ID: " << user_id << "\n\n";
// Fetch all user data in parallel using variadic when_all.
// Heterogeneous return types are flattened into the result.
std::cout << "Starting parallel fetches...\n";
auto wrap = [](auto inner) -> capy::task<capy::results<decltype(inner.await_resume())>> {
co_return capy::results<decltype(inner.await_resume())>{
std::error_code(), co_await std::move(inner)};
};
auto [ec, name, orders, balance] = co_await capy::when_all(
wrap(fetch_user_name(user_id)),
wrap(fetch_order_count(user_id)),
wrap(fetch_account_balance(user_id)));
std::cout << "\nDashboard results:\n";
std::cout << " Name: " << name << "\n";
std::cout << " Orders: " << orders << "\n";
std::cout << " Balance: $" << balance << "\n";
}
// Example with void tasks
capy::task<capy::results<>> log_access(std::string resource)
{
std::cout << "Logging access to: " << resource << "\n";
co_return capy::results<>{};
}
capy::task<capy::results<>> update_metrics(std::string metric)
{
std::cout << "Updating metric: " << metric << "\n";
co_return capy::results<>{};
}
capy::task<std::string> fetch_with_side_effects()
{
std::cout << "\n=== Fetch with side effects ===\n";
auto r = co_await capy::when_all(
log_access("api/data"),
update_metrics("api_calls"));
if (std::get<0>(r))
co_return "error";
auto data = co_await fetch_user_name(42);
std::cout << "Data: " << data << "\n";
co_return data;
}
// Error handling example
capy::task<capy::results<int>> might_fail(bool should_fail, std::string name)
{
std::cout << "Task " << name << " starting\n";
if (should_fail)
{
throw std::runtime_error(name + " failed!");
}
std::cout << "Task " << name << " completed\n";
co_return capy::results<int>{std::error_code(), 42};
}
capy::task<> demonstrate_error_handling()
{
std::cout << "\n=== Error handling ===\n";
try
{
auto [ec2, a, b, c] = co_await capy::when_all(
might_fail(false, "A"),
might_fail(true, "B"), // This one fails
might_fail(false, "C"));
std::cout << "All succeeded: " << a << ", "
<< b << ", " << c << "\n";
}
catch (std::runtime_error const& e)
{
std::cout << "Caught error: " << e.what() << "\n";
// Note: when_all waits for all tasks to complete (or respond to stop)
// before propagating the first exception
}
}
int main()
{
capy::thread_pool pool;
std::latch done(3); // std::latch - wait for 3 tasks
// Completion handlers signal the latch when each task finishes
// Use generic lambda to accept any result type (or no result for task<void>)
auto on_complete = [&done](auto&&...) { done.count_down(); };
auto on_error = [&done](std::exception_ptr) { done.count_down(); };
capy::run_async(pool.get_executor(), on_complete, on_error)(fetch_user_dashboard("alice"));
capy::run_async(pool.get_executor(), on_complete, on_error)(fetch_with_side_effects());
capy::run_async(pool.get_executor(), on_complete, on_error)(demonstrate_error_handling());
done.wait(); // Block until all tasks complete
return 0;
}
Build
add_executable(capy_example_parallel_fetch parallel_fetch.cpp)
target_link_libraries(capy_example_parallel_fetch PRIVATE Boost::capy)
Walkthrough
Basic when_all
auto [ec, name, orders, balance] = co_await capy::when_all(
wrap(fetch_user_name(user_id)),
wrap(fetch_order_count(user_id)),
wrap(fetch_account_balance(user_id)));
Payload-free children
auto r = co_await capy::when_all(
log_access("api/data"),
update_metrics("api_calls"));
if (std::get<0>(r))
co_return "error";
Error Propagation
try
{
auto [ec2, a, b, c] = co_await capy::when_all(
might_fail(false, "A"),
might_fail(true, "B"), // This one fails
might_fail(false, "C"));
std::cout << "All succeeded: " << a << ", "
<< b << ", " << c << "\n";
}
catch (std::runtime_error const& e)
{
std::cout << "Caught error: " << e.what() << "\n";
// Note: when_all waits for all tasks to complete (or respond to stop)
// before propagating the first exception
}
I/O errors are reported via ec in the results. Thrown exceptions are captured separately. Upon error, cancellation is requested and the first exception is rethrown after all tasks complete.
Output
main starts three tasks on a multi-threaded pool, and each when_all runs its children in parallel.
The line order varies from run to run.
=== Fetching dashboard for: alice ===
Fetching user ID for: alice
Got user ID: 500
Starting parallel fetches...
Fetching name for user ID: 500
Fetching order count for user: 500
Fetching balance for user: 500
Dashboard results:
Name: User500
Orders: 50
Balance: $750
=== Fetch with side effects ===
Logging access to: api/data
Updating metric: api_calls
Fetching name for user ID: 42
Data: User42
=== Error handling ===
Task A starting
Task B starting
Task C starting
Task A completed
Task C completed
Caught error: B failed!
Exercises
-
Add timing to see the parallel speedup vs sequential execution
-
Implement a "fan-out/fan-in" pattern that processes a list of items in parallel
-
Add cancellation support so remaining tasks can exit early on error
Next Steps
-
Strand Serialization: Protecting shared state with a strand instead of a mutex