nlib
nn::nlib::threading::ThreadPool Class Referencefinal

Tasks may be deployed to a pool of threads that have been already created. More...

#include "nn/nlib/threading/ThreadPool.h"

Public Member Functions

 ThreadPool () noexcept
 Instantiates the object with default parameters (default constructor). Must be initialized with Init.
 
errno_t Init (size_t thread_count, const ThreadSettings &settings) noexcept
 Starts a thread and initializes the thread pool. More...
 
errno_t Init (size_t thread_count) noexcept
 Starts a thread and initializes the thread pool. More...
 
errno_t Init () noexcept
 Starts a thread and initializes the thread pool. More...
 
 ~ThreadPool () noexcept
 Destructor. Joins all threads started internally.
 
template<class R , class FUNC >
errno_t Submit (Future< R > *future, FUNC &func) noexcept
 The task is deployed to the thread pool. More...
 
size_t Count () noexcept
 Returns the number of threads held in the thread pool.
 

Detailed Description

Tasks may be deployed to a pool of threads that have been already created.

Description
Creating a thread and destroying it for each task is a more obvious approach, but comes with a significant performance overhead cost. Creating too many concurrent threads also causes memory shortages and thrashing.
Using a thread pool reduces the overhead from creating and destroying new threads, and prevents the number of concurrent threads from growing too large.
The ThreadPool class returns Future when deploying a task, to check when a task is complete and get the return value from that task.

Definition at line 51 of file ThreadPool.h.

Member Function Documentation

◆ Init() [1/3]

nn::nlib::threading::ThreadPool::Init ( size_t  thread_count,
const ThreadSettings settings 
)
noexcept

Starts a thread and initializes the thread pool.

Parameters
[in]thread_countThe number of threads to start.
[in]settingsSetting for the thread to start.
Returns
Returns 0 on success.

◆ Init() [2/3]

nn::nlib::threading::ThreadPool::Init ( size_t  thread_count)
inlinenoexcept

Starts a thread and initializes the thread pool.

Parameters
[in]thread_countThe number of threads to start.
Returns
Returns 0 on success.

Definition at line 55 of file ThreadPool.h.

◆ Init() [3/3]

nn::nlib::threading::ThreadPool::Init ( )
inlinenoexcept

Starts a thread and initializes the thread pool.

Returns
Returns 0 on success.
Description
Runs GetHardwareConcurrency and starts the same number of threads.

Definition at line 59 of file ThreadPool.h.

◆ Submit()

template<class R , class FUNC >
errno_t nn::nlib::threading::ThreadPool::Submit ( Future< R > *  future,
FUNC &  func 
)
inlinenoexcept

The task is deployed to the thread pool.

Template Parameters
RFUNC type of the return value.
FUNCObject that does not take an argument, and calls a function pointer that returns type R.
Parameters
[out]futureThe pointer that stores the future corresponding to the deployed task (func).
[in]funcTask to deploy.
Returns
Returns 0 on success.
Description
func may be passed internally by swap for execution efficiency. As a result, the content of func is not guaranteed when passing function objects.

Definition at line 77 of file ThreadPool.h.


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