Class thread_pool

Class Documentation

class thread_pool

A threadsafe singleton thread pool class based on std::thread.

Public Functions

void set_num_threads_internal(const int)

Set the number of threads to be maintained in the pool.

int get_num_threads_internal()

Retrieve the number of threads to be maintained in the pool.

template<class Function, class ...Args, typename result_type = std::invoke_result_t<std::decay_t<Function>, std::decay_t<Args>...>>
inline std::enable_if<not std::is_void<result_type>::value, std::future<result_type>>::type internal_submit(Function &&f, Args&&... args)

Send a function with a return type to the thread pool for execution.

template<class Function, class ...Args, typename result_type = std::invoke_result_t<std::decay_t<Function>, std::decay_t<Args>...>>
inline std::enable_if<std::is_void<result_type>::value, void>::type internal_submit(Function &&f, Args&&... args)

Send a function with no return value to the thread pool for execution.

thread_pool(const thread_pool&) = delete

Uncopyable.

thread_pool &operator=(const thread_pool&) = delete

Unassignable.

Public Static Functions

static void set_num_threads(const int)

Public interface for setting the number of threads to be maintained in the pool.

static int get_num_threads()

Public interface for retrieving the number of threads to be maintained in the pool.

template<class Function, class ...Args, typename result_type = std::invoke_result_t<std::decay_t<Function>, std::decay_t<Args>...>>
static inline std::enable_if<not std::is_void<result_type>::value, std::future<result_type>>::type submit(Function &&f, Args&&... args)

Public interface for sending a function with a return type to the thread pool for execution.

template<class Function, class ...Args, typename result_type = std::invoke_result_t<std::decay_t<Function>, std::decay_t<Args>...>>
static inline std::enable_if<std::is_void<result_type>::value, void>::type submit(Function &&f, Args&&... args)

Public interface for sending a function with no return type to the thread pool for execution.

Private Functions

thread_pool()

Constructor.

~thread_pool()

Destructor.

std::pair<std::thread, std::shared_ptr<bool>> initialise_thread()

Initialise a thread-future pair.

void loop(std::shared_ptr<bool>)

Work collector. Each thread runs this indefinitely until the pool is destroyed.

Private Members

int num_threads

Number of threads to be maintained in the pool.

int num_active_threads

Number of active threads in the pool.

std::vector<std::pair<std::thread, std::shared_ptr<bool>>> threads

A vector of threads for running tasks, paired with flags indicating if they have exited their loop.

std::queue<std::function<void()>> queue

A queue of tasks waiting to be run by threads.

std::mutex queue_m

Thread lockers for the queue and the shutdown flag.

std::mutex threads_m
std::atomic<bool> shutting_down

Flag indicating that the pool is being destroyed.

Private Static Functions

static thread_pool &get_instance()

Getter for the instance; makes this class a threadsafe singleton.