20#include <system_error>
37 worker_count_ = value;
44 registration_ = value;
51 workers_ = std::move(value);
68 on_task_error_ = std::move(value);
100 return on_task_error_;
106 thread_config workers_;
132 : config_(
std::move(config)),
133 impl_(
std::make_unique<detail::thread_pool_backend>(
140 throw std::system_error(configured.error(),
"thread_pool worker configuration");
151 config_ = std::move(other.config_);
152 impl_ = std::move(other.impl_);
186 template <
typename F,
typename... Args>
191 return unexpected(std::make_error_code(std::errc::operation_canceled));
197 return impl_->try_submit(std::forward<F>(function), std::forward<Args>(args)...);
200 auto wrapped = [bound =
detail::bind_args(std::forward<F>(function), std::forward<Args>(args)...),
201 callback = std::move(callback)]()
mutable -> return_type
205 if constexpr (std::is_void_v<return_type>)
217 auto original = std::current_exception();
225 std::rethrow_exception(original);
228 return impl_->try_submit(std::move(wrapped));
236 template <
typename F,
typename... Args>
240 auto submitted =
submit(std::forward<F>(function), std::forward<Args>(args)...);
242 throw std::system_error(submitted.error(),
"thread_pool::submit");
243 return std::move(*submitted);
252 template <
typename F,
typename... Args>
257 return unexpected(std::make_error_code(std::errc::operation_canceled));
262 return impl_->try_post(std::forward<F>(function), std::forward<Args>(args)...);
265 auto wrapped = [bound =
detail::bind_args(std::forward<F>(function), std::forward<Args>(args)...),
266 callback = std::move(callback)]()
mutable
283 return impl_->try_post(std::move(wrapped));
291 template <
typename F,
typename... Args>
295 auto posted =
post(std::forward<F>(function), std::forward<Args>(args)...);
297 throw std::system_error(posted.error(),
"thread_pool::post");
307 return unexpected(std::make_error_code(std::errc::operation_canceled));
311 impl_->wait_for_tasks();
323 throw std::system_error(std::make_error_code(std::errc::operation_canceled),
"thread_pool::wait");
324 impl_->wait_for_tasks();
335 return unexpected(std::make_error_code(std::errc::operation_canceled));
369 return impl_ ? impl_->size() : 0;
374 dispose_impl() noexcept
380 if (!impl_->is_current_worker())
384 impl_->shutdown(policy);
395 auto* backend = impl_.release();
399 [backend, policy]()
noexcept
401 std::unique_ptr<detail::thread_pool_backend> owner(backend);
404 owner->shutdown(policy);
420 thread_pool_config config_;
421 std::unique_ptr<detail::thread_pool_backend> impl_;
Portable thread configuration bundle.
Builder-style configuration for thread_pool.
auto set_registration(worker_registration value) noexcept -> thread_pool_config &
Configure registry registration behavior for workers.
auto get_registration() const noexcept -> worker_registration
Return configured worker registration mode.
auto get_worker_count() const noexcept -> worker_count
Return configured worker count.
auto set_shutdown_policy(shutdown_policy value) noexcept -> thread_pool_config &
Set shutdown behavior used by thread_pool::shutdown and destructor.
auto get_shutdown_policy() const noexcept -> shutdown_policy
Return configured shutdown policy.
auto get_error_callback() const noexcept -> error_callback const &
Return configured asynchronous task error callback.
auto set_worker_count(worker_count value) noexcept -> thread_pool_config &
Set number of worker threads.
auto set_worker_config(thread_config value) -> thread_pool_config &
Set startup configuration applied to worker threads.
auto set_error_callback(error_callback value) -> thread_pool_config &
Set callback invoked when posted/submitted work throws.
auto get_worker_config() const noexcept -> thread_config const &
Return worker thread configuration.
Fixed-size thread pool for queued asynchronous work.
auto wait() -> result< void >
Wait until all queued/running tasks are finished.
thread_pool(worker_count count)
Construct a pool with explicit worker count.
auto post(F &&function, Args &&... args) -> result< void >
Post fire-and-forget work to the pool.
auto size() const noexcept -> std::size_t
Return configured worker count (0 if moved-from; retained after shutdown).
auto submit(F &&function, Args &&... args) -> result< std::future< detail::bind_result_t< F, Args... > > >
Submit work and receive a future for its result.
~thread_pool()
Shutdown the pool according to configured policy.
auto shutdown(shutdown_policy policy) -> result< void >
Shutdown with explicit policy.
static auto create(thread_pool_config config={}) -> result< thread_pool >
Create a pool without throwing.
auto configure_workers(thread_config const &config) -> result< void >
Apply thread configuration to all workers.
thread_pool()
Construct a pool with default configuration.
thread_pool(thread_pool &&) noexcept=default
void wait_or_throw()
Throwing counterpart to wait.
auto operator=(thread_pool const &) -> thread_pool &=delete
thread_pool(thread_pool_config config)
Construct a pool from full configuration.
auto submit_or_throw(F &&function, Args &&... args) -> std::future< detail::bind_result_t< F, Args... > >
Throwing counterpart to submit.
void post_or_throw(F &&function, Args &&... args)
Throwing counterpart to post.
thread_pool(thread_pool const &)=delete
auto shutdown() -> result< void >
Shutdown using configured policy.
Value type for pool worker count.
static constexpr auto automatic() noexcept -> worker_count
Create automatic worker count.
std::invoke_result_t< decltype(bind_args(std::declval< F >(), std::declval< Args >()...))& > bind_result_t
Result of invoking the decayed callable and arguments stored by bind_args.
constexpr auto to_native(shutdown_policy policy) noexcept -> shutdown_policy_backend
auto bind_args(F &&function, Args &&... args)
auto has_thread_configuration(thread_config const &config) noexcept -> bool
auto try_result(Function &&function) -> decltype(std::forward< Function >(function)())
auto global_registry() -> thread_registry &
shutdown_policy
Defines how pools handle pending work during shutdown.
@ drain
Finish queued work before returning from shutdown.
std::function< void(task_error const &)> error_callback
Callback signature for asynchronous task error reporting.
worker_registration
Controls whether pool workers are registered in a thread registry.
@ disabled
Do not register workers in the global registry.
Portable pool shutdown behavior.
static auto capture(std::string description={}) -> task_error
Capture failure context for the current exception state.
Portable task failure reporting.
Portable thread startup and runtime configuration.
Worker-thread count configuration for pool types.
Worker registration behavior for pool-managed threads.