ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
threadschedule::thread_pool Class Reference

Fixed-size thread pool for queued asynchronous work. More...

#include <thread_pool.hpp>

Public Member Functions

 thread_pool ()
 Construct a pool with default configuration.
 
 thread_pool (worker_count count)
 Construct a pool with explicit worker count.
 
 thread_pool (thread_pool_config config)
 Construct a pool from full configuration.
 
 thread_pool (thread_pool &&) noexcept=default
 
auto operator= (thread_pool &&other) noexcept -> thread_pool &
 
 thread_pool (thread_pool const &)=delete
 
auto operator= (thread_pool const &) -> thread_pool &=delete
 
 ~thread_pool ()
 Shutdown the pool according to configured policy.
 
template<typename F , typename... Args>
auto submit (F &&function, Args &&... args) -> result< std::future< detail::bind_result_t< F, Args... > > >
 Submit work and receive a future for its result.
 
template<typename F , typename... Args>
auto submit_or_throw (F &&function, Args &&... args) -> std::future< detail::bind_result_t< F, Args... > >
 Throwing counterpart to submit.
 
template<typename F , typename... Args>
auto post (F &&function, Args &&... args) -> result< void >
 Post fire-and-forget work to the pool.
 
template<typename F , typename... Args>
void post_or_throw (F &&function, Args &&... args)
 Throwing counterpart to post.
 
auto wait () -> result< void >
 Wait until all queued/running tasks are finished.
 
void wait_or_throw ()
 Throwing counterpart to wait.
 
auto configure_workers (thread_config const &config) -> result< void >
 Apply thread configuration to all workers.
 
auto shutdown () -> result< void >
 Shutdown using configured policy.
 
auto shutdown (shutdown_policy policy) -> result< void >
 Shutdown with explicit policy.
 
auto size () const noexcept -> std::size_t
 Return configured worker count (0 if moved-from; retained after shutdown).
 

Static Public Member Functions

static auto create (thread_pool_config config={}) -> result< thread_pool >
 Create a pool without throwing.
 

Detailed Description

Fixed-size thread pool for queued asynchronous work.

Definition at line 114 of file thread_pool.hpp.

Constructor & Destructor Documentation

◆ thread_pool() [1/5]

threadschedule::thread_pool::thread_pool ( )
inline

Construct a pool with default configuration.

Definition at line 118 of file thread_pool.hpp.

◆ thread_pool() [2/5]

threadschedule::thread_pool::thread_pool ( worker_count  count)
inlineexplicit

Construct a pool with explicit worker count.

Parameters
countNumber of workers or automatic resolution mode.

Definition at line 124 of file thread_pool.hpp.

◆ thread_pool() [3/5]

threadschedule::thread_pool::thread_pool ( thread_pool_config  config)
inlineexplicit

Construct a pool from full configuration.

Parameters
configPool configuration.
Exceptions
std::system_errorif worker configuration cannot be applied.

Definition at line 131 of file thread_pool.hpp.

References threadschedule::thread_pool_config::get_worker_config(), threadschedule::detail::has_thread_configuration(), and threadschedule::detail::to_native().

◆ thread_pool() [4/5]

threadschedule::thread_pool::thread_pool ( thread_pool &&  )
defaultnoexcept

◆ thread_pool() [5/5]

threadschedule::thread_pool::thread_pool ( thread_pool const &  )
delete

◆ ~thread_pool()

threadschedule::thread_pool::~thread_pool ( )
inline

Shutdown the pool according to configured policy.

Destructor never throws and will attempt best-effort shutdown.

Definition at line 175 of file thread_pool.hpp.

Member Function Documentation

◆ configure_workers()

auto threadschedule::thread_pool::configure_workers ( thread_config const &  config) -> result<void>
inline

Apply thread configuration to all workers.

Parameters
configPortable configuration to apply.

Definition at line 332 of file thread_pool.hpp.

References threadschedule::detail::to_native(), and threadschedule::detail::try_result().

◆ create()

static auto threadschedule::thread_pool::create ( thread_pool_config  config = {}) -> result<thread_pool>
inlinestatic

Create a pool without throwing.

Parameters
configPool configuration.
Returns
A ready-to-use pool or a translated error code.

Definition at line 165 of file thread_pool.hpp.

◆ operator=() [1/2]

auto threadschedule::thread_pool::operator= ( thread_pool &&  other) -> thread_pool&
inlinenoexcept

Definition at line 146 of file thread_pool.hpp.

◆ operator=() [2/2]

auto threadschedule::thread_pool::operator= ( thread_pool const &  ) -> thread_pool &=delete
delete

◆ post()

template<typename F , typename... Args>
auto threadschedule::thread_pool::post ( F &&  function,
Args &&...  args 
) -> result<void>
inline

Post fire-and-forget work to the pool.

Parameters
functionCallable to execute.
argsArguments forwarded to the callable.
Returns
Success or error code.

Definition at line 254 of file thread_pool.hpp.

References threadschedule::detail::bind_args(), threadschedule::task_error::capture(), threadschedule::thread_pool_config::get_error_callback(), and threadschedule::detail::try_result().

Referenced by post_or_throw().

◆ post_or_throw()

template<typename F , typename... Args>
void threadschedule::thread_pool::post_or_throw ( F &&  function,
Args &&...  args 
)
inline

Throwing counterpart to post.

Exceptions
std::system_errorif posting fails.

Definition at line 293 of file thread_pool.hpp.

References post().

◆ shutdown() [1/2]

auto threadschedule::thread_pool::shutdown ( ) -> result<void>
inline

Shutdown using configured policy.

Definition at line 343 of file thread_pool.hpp.

References threadschedule::thread_pool_config::get_shutdown_policy(), and shutdown().

Referenced by shutdown().

◆ shutdown() [2/2]

auto threadschedule::thread_pool::shutdown ( shutdown_policy  policy) -> result<void>
inline

Shutdown with explicit policy.

Parameters
policyDrain/cancel behavior used for pending tasks.

Definition at line 353 of file thread_pool.hpp.

References threadschedule::detail::to_native(), and threadschedule::detail::try_result().

◆ size()

auto threadschedule::thread_pool::size ( ) const -> std::size_t
inlinenoexcept

Return configured worker count (0 if moved-from; retained after shutdown).

Definition at line 367 of file thread_pool.hpp.

◆ submit()

template<typename F , typename... Args>
auto threadschedule::thread_pool::submit ( F &&  function,
Args &&...  args 
) -> result<std::future<detail::bind_result_t<F, Args...>>>
inline

Submit work and receive a future for its result.

Parameters
functionCallable to execute on a worker.
argsArguments forwarded to the callable.
Returns
A future on success, or an error (for example canceled/queue rejection).

Definition at line 188 of file thread_pool.hpp.

References threadschedule::detail::bind_args(), threadschedule::task_error::capture(), threadschedule::thread_pool_config::get_error_callback(), and threadschedule::detail::try_result().

Referenced by submit_or_throw().

◆ submit_or_throw()

template<typename F , typename... Args>
auto threadschedule::thread_pool::submit_or_throw ( F &&  function,
Args &&...  args 
) -> std::future<detail::bind_result_t<F, Args...>>
inline

Throwing counterpart to submit.

Exceptions
std::system_errorif submission fails.

Definition at line 238 of file thread_pool.hpp.

References submit().

◆ wait()

auto threadschedule::thread_pool::wait ( ) -> result<void>
inline

Wait until all queued/running tasks are finished.

Definition at line 304 of file thread_pool.hpp.

References threadschedule::detail::try_result().

◆ wait_or_throw()

void threadschedule::thread_pool::wait_or_throw ( )
inline

Throwing counterpart to wait.

Definition at line 320 of file thread_pool.hpp.


The documentation for this class was generated from the following file: