← API Reference Class

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.

Note

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.

See also

Worker — consumes jobs from the queue.

See also

WorkerJob — wraps a callable into an IExecuter.

Private Attributes

#_internal::job_queue m_Queueprivate
#std::vector< _internal::Worker > m_Workersprivate
#context_pointer m_Ctxprivate

Public Member Functions

#ThreadPool(std::size_t nWorkers=HardwareConcurrency())
#ThreadPool(ThreadPool &&)=delete
#ThreadPool(ThreadPool const &)=delete
#ThreadPool & operator=(ThreadPool const &)=delete
#ThreadPool & 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.

Parameters
inFunc

The input function

inArgs

Arguments to be passed to the function

Template Parameters
_Callable

The input function type

_Args

The input variadic arguments types pack

#context_pointer & GetPoolContext() noexcept