HIPO4 C++ Library 4.4.1
Columnar I/O library for CLAS12 physics data
Loading...
Searching...
No Matches
ThreadPool Class Reference

A ThreadPool implementation. More...

#include <threadpool.hpp>

Classes

struct  Statistics
 

Public Types

enum class  ShutdownResult { Graceful , Forced , AlreadyStopped }
 

Public Member Functions

 ThreadPool (std::size_t num_threads=0, std::string_view thread_name_prefix="ThreadPool")
 Constructs a ThreadPool with specified number of worker threads.
 
 ~ThreadPool () noexcept
 Destructor ensures graceful shutdown with reasonable timeout.
 
 ThreadPool (const ThreadPool &)=delete
 
ThreadPooloperator= (const ThreadPool &)=delete
 
 ThreadPool (ThreadPool &&) noexcept
 
ThreadPooloperator= (ThreadPool &&) noexcept
 
template<typename Callable , typename... Args>
auto submit (Callable &&callable, Args &&... args) -> std::future< std::invoke_result_t< std::decay_t< Callable >, std::decay_t< Args >... > >
 Submits a task for asynchronous execution.
 
auto shutdown (std::chrono::milliseconds timeout=std::chrono::seconds{5}) noexcept -> void
 Initiates graceful shutdown with specified timeout.
 
auto wait_for_idle (std::chrono::milliseconds timeout=std::chrono::milliseconds::max()) const -> bool
 Waits for all pending tasks to complete.
 
auto thread_count () const noexcept -> std::size_t
 Gets the number of worker threads.
 
auto pending_tasks () const noexcept -> std::size_t
 Gets the number of pending tasks.
 
auto is_running () const noexcept -> bool
 Checks if the pool is running (not stopped).
 
auto get_statistics () const noexcept -> const Statistics &
 Gets current statistics snapshot.
 

Detailed Description

A ThreadPool implementation.

Member Enumeration Documentation

◆ ShutdownResult

enum class ThreadPool::ShutdownResult
strong
Enumerator
Graceful 

All tasks completed within timeout.

Forced 

Timeout exceeded, threads detached.

AlreadyStopped 

Pool was already stopped.

Constructor & Destructor Documentation

◆ ThreadPool() [1/3]

ThreadPool::ThreadPool ( std::size_t  num_threads = 0,
std::string_view  thread_name_prefix = "ThreadPool" 
)
inlineexplicit

Constructs a ThreadPool with specified number of worker threads.

Parameters
num_threadsNumber of threads (0 = hardware_concurrency)
thread_name_prefixPrefix for thread names
Exceptions
std::system_errorif thread creation fails
std::invalid_argumentif num_threads > hardware limit

◆ ~ThreadPool()

ThreadPool::~ThreadPool ( )
inlinenoexcept

Destructor ensures graceful shutdown with reasonable timeout.

◆ ThreadPool() [2/3]

ThreadPool::ThreadPool ( const ThreadPool )
delete

◆ ThreadPool() [3/3]

ThreadPool::ThreadPool ( ThreadPool &&  other)
inlinenoexcept

Member Function Documentation

◆ operator=() [1/2]

ThreadPool & ThreadPool::operator= ( const ThreadPool )
delete

◆ operator=() [2/2]

ThreadPool & ThreadPool::operator= ( ThreadPool &&  other)
inlinenoexcept

◆ submit()

template<typename Callable , typename... Args>
auto ThreadPool::submit ( Callable &&  callable,
Args &&...  args 
) -> std::future<std::invoke_result_t<std::decay_t<Callable>, std::decay_t<Args>...>>

Submits a task for asynchronous execution.

Parameters
callableFunction/callable object to execute
argsArguments to pass to the callable
Returns
Future for the result
Exceptions
std::runtime_errorif pool is stopped

◆ shutdown()

auto ThreadPool::shutdown ( std::chrono::milliseconds  timeout = std::chrono::seconds{5}) -> void
inlinenoexcept

Initiates graceful shutdown with specified timeout.

Parameters
timeoutMaximum time to wait for completion

◆ wait_for_idle()

auto ThreadPool::wait_for_idle ( std::chrono::milliseconds  timeout = std::chrono::milliseconds::max()) const -> bool
inline

Waits for all pending tasks to complete.

Parameters
timeoutMaximum time to wait
Returns
true if all tasks completed within timeout

◆ thread_count()

auto ThreadPool::thread_count ( ) const -> std::size_t
inlinenoexcept

Gets the number of worker threads.

◆ pending_tasks()

auto ThreadPool::pending_tasks ( ) const -> std::size_t
inlinenoexcept

Gets the number of pending tasks.

◆ is_running()

auto ThreadPool::is_running ( ) const -> bool
inlinenoexcept

Checks if the pool is running (not stopped).

◆ get_statistics()

auto ThreadPool::get_statistics ( ) const -> const Statistics&
inlinenoexcept

Gets current statistics snapshot.


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