17#include <system_error>
49 template <
typename F,
typename... Args>
50 auto submit(F&& f, Args&&... args) -> std::future<std::invoke_result_t<F, Args...>>
52 auto result =
try_submit(std::forward<F>(f), std::forward<Args>(args)...);
53 if (!result.has_value())
54 throw std::runtime_error(
"InlinePool is shut down");
55 return std::move(result.value());
58 template <
typename F,
typename... Args>
59 auto try_submit(F&& f, Args&&... args) ->
expected<std::future<std::invoke_result_t<F, Args...>>, std::error_code>
61 using R = std::invoke_result_t<F, Args...>;
63 return unexpected(std::make_error_code(std::errc::operation_canceled));
66 auto future = p.get_future();
69 if constexpr (std::is_void_v<R>)
71 std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
76 p.set_value(std::invoke(std::forward<F>(f), std::forward<Args>(args)...));
81 p.set_exception(std::current_exception());
86 template <
typename F,
typename... Args>
87 void post(F&& f, Args&&... args)
89 auto r =
try_post(std::forward<F>(f), std::forward<Args>(args)...);
91 throw std::runtime_error(
"InlinePool is shut down");
94 template <
typename F,
typename... Args>
98 return unexpected(std::make_error_code(std::errc::operation_canceled));
101 std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
109 template <
typename Iterator>
113 return unexpected(std::make_error_code(std::errc::operation_canceled));
115 std::vector<std::future<void>> futures;
116 for (
auto it = begin; it != end; ++it)
118 std::promise<void> p;
119 futures.push_back(p.get_future());
127 p.set_exception(std::current_exception());
133 template <
typename Iterator>
134 auto submit_batch(Iterator begin, Iterator end) -> std::vector<std::future<void>>
137 if (!result.has_value())
138 throw std::runtime_error(
"InlinePool is shut down");
139 return std::move(result.value());
142 template <
typename Iterator,
typename F>
145 for (
auto it = begin; it != end; ++it)
149 [[nodiscard]]
auto size() const noexcept ->
size_t {
return 0; }
auto submit(F &&f, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > >
auto pending_tasks() const noexcept -> size_t
void shutdown(ShutdownPolicy=ShutdownPolicy::drain)
auto size() const noexcept -> size_t
auto try_post(F &&f, Args &&... args) -> expected< void, std::error_code >
auto try_submit_batch(Iterator begin, Iterator end) -> expected< std::vector< std::future< void > >, std::error_code >
void post(F &&f, Args &&... args)
void parallel_for_each(Iterator begin, Iterator end, F &&func)
auto try_submit(F &&f, Args &&... args) -> expected< std::future< std::invoke_result_t< F, Args... > >, std::error_code >
auto submit_batch(Iterator begin, Iterator end) -> std::vector< std::future< void > >
A result type that holds either a value of type T or an error of type E.
Exception thrown by expected::value() when the object is in the error state.
Polyfill for std::expected (C++23) for pre-C++23 compilers.
ShutdownPolicy
Controls how a pool handles pending tasks during shutdown.
@ drain
Finish all queued tasks before stopping (default).
Thread pools: HighPerformancePool, ThreadPoolBase, LightweightPoolT, and GlobalPool.