asge::concurrent::ThreadPool
A fixed-size thread pool that executes submitted jobs concurrently.
#include <ThreadPool.hpp>ThreadPool manages a collection of Worker threads and a shared lock-free MPMC queue. Callers submit work via Submit(), which wraps the callable and its arguments into a WorkerJob, enqueues it, and returns either a std::future<T> (if the callable has a non-void return type) or nothing (if it returns void).
The pool is intentionally non-movable and non-copyable: it owns both the queue and the workers, and Workers hold a reference to the queue, so relocating the pool would silently invalidate those references.
Lifetime: the destructor is responsible for signalling workers to stop and joining all threads before the queue is destroyed.
The default worker count is HardwareConcurrency(), which maps to std::thread::hardware_concurrency(). Consider passing an explicit count for latency-sensitive or IO-heavy workloads.
Worker — consumes jobs from the queue.
WorkerJob — wraps a callable into an IExecuter.
Private Attributes
Public Member Functions
ThreadPool(std::size_t nWorkers=HardwareConcurrency())ThreadPool(ThreadPool &&)=deleteThreadPool(ThreadPool const &)=deleteThreadPool & operator=(ThreadPool const &)=deleteThreadPool & operator=(ThreadPool &&)=delete~ThreadPool()template<typename _Callable, typename ... _Args> auto Submit(_Callable &&inFunc, _Args &&...inArgs) -> _internal::future_or_void_t< _Callable >Submits a work to the Thread Pool.
The works cames into the format of a callable with respective arguments. It is then converted into a Worker Job and put inside the queue, which will be eventually executed by one of the workers. If the input callable also presents a return value, then a std::future<T> is returned, void otherwise.
inFunc | The input function |
inArgs | Arguments to be passed to the function |
_Callable | The input function type |
_Args | The input variadic arguments types pack |
context_pointer & GetPoolContext() noexcept