34template<
typename F,
typename R>
38 auto job_ptr = std::make_unique<future_job<R>>(
39 std::forward<F>(callable),
40 opts.name.empty() ?
"async_job" : opts.name
43 auto future = job_ptr->get_future();
45 auto result = enqueue(std::move(job_ptr));
47 std::promise<R> error_promise;
48 error_promise.set_exception(
49 std::make_exception_ptr(
50 std::runtime_error(
result.error().message)
53 return error_promise.get_future();
59template<
typename F,
typename R>
61 -> std::vector<std::future<R>>
65 single_opts.
name = opts.name;
66 return submit<F, R>(std::move(callable), single_opts);
70template<
typename F,
typename R>
74 auto futures = submit<F, R>(std::move(callables), opts);
78template<
typename F,
typename R>
82 if (callables.empty()) {
86 auto futures = submit<F, R>(std::move(callables), opts);
87 auto completed = std::make_shared<std::atomic<bool>>(
false);
88 auto result_promise = std::make_shared<std::promise<R>>();
89 auto result_future = result_promise->get_future();
91 for (std::size_t i = 0; i < futures.size(); ++i) {
92 std::thread([
completed, result_promise, fut = std::move(futures[i])]()
mutable {
95 bool expected =
false;
96 if (
completed->compare_exchange_strong(expected,
true)) {
97 result_promise->set_value(std::move(
result));
100 bool expected =
false;
101 if (
completed->compare_exchange_strong(expected,
true)) {
102 result_promise->set_exception(std::current_exception());
109 return common::Result<R>::ok(result_future.get());
110 }
catch (
const std::exception& e) {
120 std::scoped_lock<std::mutex> lock(policies_mutex_);
122 for (
auto& policy : policies_) {
123 if (policy && policy->get_name() == name) {
124 return dynamic_cast<T*
>(policy.get());
Helper templates for batch operations to eliminate duplicated loop patterns.
A template class representing either a value or an error.
auto submit_wait_any(std::vector< F > &&callables, const submit_options &opts={}) -> common::Result< R >
Submit a batch and return first completed result.
auto submit_wait_all(std::vector< F > &&callables, const submit_options &opts={}) -> std::vector< R >
Submit a batch and wait for all results.
auto find_policy(const std::string &name) -> T *
Find a policy by name.
auto submit(F &&callable, const submit_options &opts={}) -> std::future< R >
Job wrapper that provides std::future for async result returns.
Error codes and utilities for the thread system.
auto collect_all(std::vector< std::future< T > > &futures) -> std::vector< T >
Collect all results from a vector of futures.
auto batch_apply(Container &&items, Operation &&op)
Apply an operation to each item in a collection, returning results.
Core threading foundation of the thread system library.
common::VoidResult make_error_result(error_code code, const std::string &message="")
Create a common::VoidResult error from a thread::error_code.
@ completed
Successfully completed.
Options for submitting jobs to the thread pool.
std::string name
Optional name for the job (useful for debugging/tracing).