jubilant-funicular
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
nta::utils::ThreadPool Class Reference

Collection of threads for running scheduled functions. More...

#include <ThreadPool.h>

Collaboration diagram for nta::utils::ThreadPool:
Collaboration graph
[legend]

Classes

struct  worker
 One thread available to the pool. More...
 

Public Member Functions

 ThreadPool (size_t num_threads)
 Constructs a ThreadPool with specified number of threads.
 
void schedule (const Thunk &thunk)
 Schedules a function to be executed.
 
void wait ()
 Waits for all scheduled functions to finish execution.
 
 ThreadPool (const ThreadPool &)=delete
 
ThreadPooloperator= (const ThreadPool &)=delete
 

Private Member Functions

void dispatcher ()
 Function run by dispatch thread. More...
 
void worker_func (size_t wid)
 Function run by worker threads.
 
size_t getAvailableWorker ()
 Returns id of an available worker.
 
bool allWorkFinished ()
 Returns whether or not all work is finished.
 

Private Attributes

struct {
std::condition_variable_any cv
 
std::mutex m
 
m_empty
 Used to keep track of when the pool is empty.
 
std::vector< workerm_workers
 The workers.
 
std::atomic< bool > m_kill
 Sign to kill the pool.
 
Semaphore m_scheduled
 Record of scheduled workers.
 
Semaphore m_available
 Record of available workers.
 
std::queue< Thunk > m_funcs
 The functions to be executed.
 
std::thread m_dispatch_thread
 The thread for managing the pool.
 

Detailed Description

Collection of threads for running scheduled functions.

Definition at line 36 of file ThreadPool.h.

Member Function Documentation

◆ dispatcher()

void nta::utils::ThreadPool::dispatcher ( )
private

Function run by dispatch thread.

Todo:
Can I reorganize to replace with a while(!kill)?

Definition at line 28 of file ThreadPool.cpp.

Referenced by ThreadPool().


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