A ThreadPool implementation.
More...
#include <threadpool.hpp>
|
| | 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 |
| |
| ThreadPool & | operator= (const ThreadPool &)=delete |
| |
| | ThreadPool (ThreadPool &&) noexcept |
| |
| ThreadPool & | operator= (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.
|
| |
A ThreadPool implementation.
◆ ShutdownResult
| Enumerator |
|---|
| Graceful | All tasks completed within timeout.
|
| Forced | Timeout exceeded, threads detached.
|
| AlreadyStopped | Pool was already stopped.
|
◆ 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_threads | Number of threads (0 = hardware_concurrency) |
| thread_name_prefix | Prefix for thread names |
- Exceptions
-
| std::system_error | if thread creation fails |
| std::invalid_argument | if num_threads > hardware limit |
◆ ~ThreadPool()
| ThreadPool::~ThreadPool |
( |
| ) |
|
|
inlinenoexcept |
Destructor ensures graceful shutdown with reasonable timeout.
◆ ThreadPool() [2/3]
◆ ThreadPool() [3/3]
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ 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
-
| callable | Function/callable object to execute |
| args | Arguments to pass to the callable |
- Returns
- Future for the result
- Exceptions
-
| std::runtime_error | if pool is stopped |
◆ shutdown()
| auto ThreadPool::shutdown |
( |
std::chrono::milliseconds |
timeout = std::chrono::seconds{5} | ) |
-> void |
|
inlinenoexcept |
Initiates graceful shutdown with specified timeout.
- Parameters
-
| timeout | Maximum 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
-
| timeout | Maximum 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: