ThreadPool Doxygen¶
Public ThreadPool¶
-
class
ThreadPool
ThreadPool implement a multiple queues/multiple workers threadpool.
When created, the pool will start the workers(threads) immediatly. The threads will only terminate when the pool is destroyed.
This class implements a one queue per worker strategy to dispatch work.
Public Functions
-
ThreadPool
() Constructs a ThreadPool.
The number of workers will be deduced from hardware.
-
ThreadPool
(std::size_t pool_size) Constructs a ThreadPool.
- Parameters
pool_size
: Number of threads to start.
-
ThreadPool
(std::shared_ptr<Hooks> hooks) Constructs a ThreadPool.
- Parameters
hooks
: Hooks to register in the pool.
-
ThreadPool
(std::size_t pool_size, std::shared_ptr<Hooks> hooks) Constructs a ThreadPool.
- Parameters
pool_size
: Number of threads to start.hooks
: Hooks to register in the pool.
-
~ThreadPool
() Stops the pool and clean all workers.
-
template <typename Function, typename... Args>
autorun
(Function &&f, Args&&... args) Run a task in the SingleQueue.
When a task is ran in the SingleQueue, the callable object will be packaged in a packaged_task and put in the inner task_queue. A waiting worker will pick the task and execute it. If no workers are available, the task will remain in the queue until a worker picks it up.
- Return
- Returns a future containing the result of the task.
-
void
stop
() Stop the ThreadPool.
A stopped ThreadPool will discard any task dispatched to it. All workers will discard new tasks, but the threads will not exit.
-
void
register_hooks
(std::shared_ptr<Hooks> hooks) Register a ThreadPool::Hooks class.
- Parameters
hooks
: The class to be registered
-
bool
is_stopped
() const Check the state of the threadpool.
- Return
- True if the ThreadPool is stopped, false otherwise.
-
std::size_t
threads_available
() const Check on the number of threads not currently working.
The number might be imprecise, as between the time the value is read and returned, a thread might become unavailable.
- Return
- The number of threads currently waiting for a task.
-
std::size_t
threads_working
() const Check on the number of threads currently working.
The number might be imprecise, as between the time the value is read and returned, a thread might finish a task and become available.
- Return
- The number of threads currently working.
-
Private ThreadPool¶
-
class
ThreadPool
ThreadPool implement a multiple queues/multiple workers threadpool.
When created, the pool will start the workers(threads) immediatly. The threads will only terminate when the pool is destroyed.
This class implements a one queue per worker strategy to dispatch work.
Private Functions
-
void
start_pool
() Starts the pool when the pool is constructed.
It will starts _pool_size threads.
-
void
clean
() Clean the pool and join threads of dead workers.
This method may be called at any time by any thread putting a job in the queue. This function acquires a lock on the pool vector.
-
void
terminate
() Joins all threads in the pool.
Should only be called from destructor. This method will stop all the worker and join the corresponding thread.
-
std::size_t
get_dispatch_worker
() Find the worker for which to dispatch the tasks.
- Return
- The index in the worker array to which a task should be dispatch.
-
template <typename TaskType>
voiddispatch_work
(const std::size_t idx, TaskType task) Dispatch a task to a given worker.
- Parameters
idx
: Index of the worker to dispatch the work attask
: Task to dispatch into the worker
-
void
check_spawn_single_worker
() Check if the pool can spawn more workers, and spawn one for a single task.
It will check the current number of spawned threads and if it can spawn or not a new thread. If a thread can be spawned, one is created.
Private Members
-
std::vector<std::pair<std::thread, std::unique_ptr<Worker>>>
pool
Vector of thread, the actual thread pool.
Emplacing in this vector construct and launch a thread.
-
std::mutex
pool_lock
Mutex regulating acces to the pool.
-
std::atomic<std::size_t>
waiting_threads
Number of waiting threads in the pool.
-
std::atomic<std::size_t>
working_threads
Number of threads executing a task in the pool.
-
std::atomic<bool>
stopped
Boolean representing if the pool is stopped.
-
std::shared_ptr<Hooks>
hooks
Struct containing all hooks the threadpool will call.
-
const std::size_t
pool_size
Size of the pool.
-
struct
Worker
Inner worker class. Capture the ThreadPool when built.
The only job of this class is to run tasks. It will use the captured ThreadPool to interact with it.
Private Members
-
ThreadPool *
pool
Captured ThreadPool that the worker works for.
-
ThreadPool *
-
void