nlib
nn::nlib::threading::LockFreeThreadPool< N > Class Template Referencefinal

This thread pool is suitable for deploying fine grain tasks. More...

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

Inherits nn::nlib::threading::detail::LockFreeThreadPoolBase.

Public Types

typedef void *(* Func) (void **args, size_t n)
 Type for functions that can be deployed to the thread pool. More...
 
typedef void(* Callback) (void *result)
 Type for callback functions called when a task ends. More...
 

Public Member Functions

 LockFreeThreadPool () noexcept
 Instantiates the object with default parameters (default constructor). Must be initialized with Init.
 
 ~LockFreeThreadPool () noexcept
 Destructor. Joins all threads started internally.
 
errno_t Init (size_t work_queue_size, size_t thread_count, const ThreadSettings &settings) noexcept
 Starts a thread and initializes the thread pool. More...
 
errno_t SubmitVarArgs (Func func, Callback callback, size_t n,...) noexcept
 A task is deployed to the thread pool by specifying the number of arguments of the function. More...
 
errno_t Submit (Func func, Callback callback)
 A task is deployed to the thread pool without specifying the arguments of the function. For more information, see SubmitVarArgs(). More...
 
errno_t Submit (Func func, Callback callback, void *arg0)
 A task is deployed to the thread pool by specifying one argument to the function. For more information, see SubmitVarArgs(). More...
 
errno_t Submit (Func func, Callback callback, void *arg0, void *arg1)
 A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs(). More...
 
errno_t Submit (Func func, Callback callback, void *arg0, void *arg1, void *arg2)
 A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs(). More...
 
errno_t Submit (Func func, Callback callback, void *arg0, void *arg1, void *arg2, void *arg3)
 A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs(). More...
 
errno_t Submit (Func func, Callback callback, void *arg0, void *arg1, void *arg2, void *arg3, void *arg4)
 A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs(). More...
 
errno_t Submit (Func func, Callback callback, void *arg0, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5)
 A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs(). More...
 
errno_t Submit (Func func, Callback callback, void *arg0, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6)
 A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs(). More...
 
errno_t Submit (Func func, Callback callback, void *arg0, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6, void *arg7)
 A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs(). More...
 
errno_t Submit (Func func, Callback callback, void *arg0, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6, void *arg7, void *arg8)
 A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs(). More...
 
errno_t Submit (Func func, Callback callback, void *arg0, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6, void *arg7, void *arg8, void *arg9)
 A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs(). More...
 
size_t GetThreadCount ()
 Returns the number of threads held in the thread pool.
 

Detailed Description

template<size_t N = 5>
class nn::nlib::threading::LockFreeThreadPool< N >

This thread pool is suitable for deploying fine grain tasks.

Template Parameters
NThe maximum number of arguments that a function deployed in the thread pool can accept.
Description
A lock-free queue is used for a queue that provides tasks to threads of the thread pool to eliminate overhead caused by a lock required for deploying and consuming tasks. In addition, allowing only one function type to be tasks to deploy reduces overheads, such as dynamic memory allocation, associated with type erasure, and virtual function calls. However, when no task to be processed is available, overhead during idle time increases because threads of the thread pool wait for tasks using nlib_sleep() or nlib_yield().

Definition at line 127 of file ThreadPool.h.

Member Typedef Documentation

◆ Callback

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Callback

Type for callback functions called when a task ends.

Parameters
[in]resultReturn value of Func.

Definition at line 131 of file ThreadPool.h.

◆ Func

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Func

Type for functions that can be deployed to the thread pool.

Parameters
[in]argsPointer to a void* type pointer array.
[in]nThe number of void* type pointers stored in args.
Description
Note that n matches the template argument N.

Definition at line 130 of file ThreadPool.h.

Member Function Documentation

◆ Init()

template<size_t N>
errno_t nn::nlib::threading::LockFreeThreadPool< N >::Init ( size_t  work_queue_size,
size_t  thread_count,
const ThreadSettings settings 
)
noexcept

Starts a thread and initializes the thread pool.

Parameters
[in]work_queue_sizeThe size of a queue to store deployed tasks.
[in]thread_countThe number of threads to start.
[in]settingsSettings for threads to start.
Return values
0Success.
EINVALIndicates that thread_count is 0 or greater than 65535.
ENOMEMmemory allocation failed.
EALREADYAlready initialized.
Failedto create a thread for other reasons.

Definition at line 357 of file ThreadPool.h.

◆ Submit() [1/11]

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Submit ( Func  func,
Callback  callback 
)

A task is deployed to the thread pool without specifying the arguments of the function. For more information, see SubmitVarArgs().

Parameters
[in]funcFunction to run in the thread pool.
[in]callbackCallback function that takes the return value of func as its argument.
Return values
0Success.
EAGAINThe queue is full.

◆ Submit() [2/11]

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Submit ( Func  func,
Callback  callback,
void *  arg0 
)

A task is deployed to the thread pool by specifying one argument to the function. For more information, see SubmitVarArgs().

Parameters
[in]funcFunction to run in the thread pool.
[in]callbackCallback function that takes the return value of func as its argument.
[in]arg0Argument to a function run in the thread pool.
Return values
0Success.
EAGAINThe queue is full.

◆ Submit() [3/11]

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Submit ( Func  func,
Callback  callback,
void *  arg0,
void *  arg1 
)

A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs().

Parameters
[in]funcFunction to run in the thread pool.
[in]callbackCallback function that takes the return value of func as its argument.
[in]arg0Argument to a function run in the thread pool.
[in]arg1Argument to a function run in the thread pool.
Return values
0Success.
EAGAINThe queue is full.

◆ Submit() [4/11]

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Submit ( Func  func,
Callback  callback,
void *  arg0,
void *  arg1,
void *  arg2 
)

A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs().

Parameters
[in]funcFunction to run in the thread pool.
[in]callbackCallback function that takes the return value of func as its argument.
[in]arg0Argument to a function run in the thread pool.
[in]arg1Argument to a function run in the thread pool.
[in]arg2Argument to a function run in the thread pool.
Return values
0Success.
EAGAINThe queue is full.

◆ Submit() [5/11]

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Submit ( Func  func,
Callback  callback,
void *  arg0,
void *  arg1,
void *  arg2,
void *  arg3 
)

A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs().

Parameters
[in]funcFunction to run in the thread pool.
[in]callbackCallback function that takes the return value of func as its argument.
[in]arg0Argument to a function run in the thread pool.
[in]arg1Argument to a function run in the thread pool.
[in]arg2Argument to a function run in the thread pool.
[in]arg3Argument to a function run in the thread pool.
Return values
0Success.
EAGAINThe queue is full.

◆ Submit() [6/11]

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Submit ( Func  func,
Callback  callback,
void *  arg0,
void *  arg1,
void *  arg2,
void *  arg3,
void *  arg4 
)

A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs().

Parameters
[in]funcFunction to run in the thread pool.
[in]callbackCallback function that takes the return value of func as its argument.
[in]arg0Argument to a function run in the thread pool.
[in]arg1Argument to a function run in the thread pool.
[in]arg2Argument to a function run in the thread pool.
[in]arg3Argument to a function run in the thread pool.
[in]arg4Argument to a function run in the thread pool.
Return values
0Success.
EAGAINThe queue is full.

◆ Submit() [7/11]

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Submit ( Func  func,
Callback  callback,
void *  arg0,
void *  arg1,
void *  arg2,
void *  arg3,
void *  arg4,
void *  arg5 
)

A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs().

Parameters
[in]funcFunction to run in the thread pool.
[in]callbackCallback function that takes the return value of func as its argument.
[in]arg0Argument to a function run in the thread pool.
[in]arg1Argument to a function run in the thread pool.
[in]arg2Argument to a function run in the thread pool.
[in]arg3Argument to a function run in the thread pool.
[in]arg4Argument to a function run in the thread pool.
[in]arg5Argument to a function run in the thread pool.
Return values
0Success.
EAGAINThe queue is full.

◆ Submit() [8/11]

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Submit ( Func  func,
Callback  callback,
void *  arg0,
void *  arg1,
void *  arg2,
void *  arg3,
void *  arg4,
void *  arg5,
void *  arg6 
)

A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs().

Parameters
[in]funcFunction to run in the thread pool.
[in]callbackCallback function that takes the return value of func as its argument.
[in]arg0Argument to a function run in the thread pool.
[in]arg1Argument to a function run in the thread pool.
[in]arg2Argument to a function run in the thread pool.
[in]arg3Argument to a function run in the thread pool.
[in]arg4Argument to a function run in the thread pool.
[in]arg5Argument to a function run in the thread pool.
[in]arg6Argument to a function run in the thread pool.
Return values
0Success.
EAGAINThe queue is full.

◆ Submit() [9/11]

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Submit ( Func  func,
Callback  callback,
void *  arg0,
void *  arg1,
void *  arg2,
void *  arg3,
void *  arg4,
void *  arg5,
void *  arg6,
void *  arg7 
)

A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs().

Parameters
[in]funcFunction to run in the thread pool.
[in]callbackCallback function that takes the return value of func as its argument.
[in]arg0Argument to a function run in the thread pool.
[in]arg1Argument to a function run in the thread pool.
[in]arg2Argument to a function run in the thread pool.
[in]arg3Argument to a function run in the thread pool.
[in]arg4Argument to a function run in the thread pool.
[in]arg5Argument to a function run in the thread pool.
[in]arg6Argument to a function run in the thread pool.
[in]arg7Argument to a function run in the thread pool.
Return values
0Success.
EAGAINThe queue is full.

◆ Submit() [10/11]

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Submit ( Func  func,
Callback  callback,
void *  arg0,
void *  arg1,
void *  arg2,
void *  arg3,
void *  arg4,
void *  arg5,
void *  arg6,
void *  arg7,
void *  arg8 
)

A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs().

Parameters
[in]funcFunction to run in the thread pool.
[in]callbackCallback function that takes the return value of func as its argument.
[in]arg0Argument to a function run in the thread pool.
[in]arg1Argument to a function run in the thread pool.
[in]arg2Argument to a function run in the thread pool.
[in]arg3Argument to a function run in the thread pool.
[in]arg4Argument to a function run in the thread pool.
[in]arg5Argument to a function run in the thread pool.
[in]arg6Argument to a function run in the thread pool.
[in]arg7Argument to a function run in the thread pool.
[in]arg8Argument to a function run in the thread pool.
Return values
0Success.
EAGAINThe queue is full.

◆ Submit() [11/11]

template<size_t N = 5>
nn::nlib::threading::LockFreeThreadPool< N >::Submit ( Func  func,
Callback  callback,
void *  arg0,
void *  arg1,
void *  arg2,
void *  arg3,
void *  arg4,
void *  arg5,
void *  arg6,
void *  arg7,
void *  arg8,
void *  arg9 
)

A task is deployed to the thread pool by specifying two arguments to the function. For more information, see SubmitVarArgs().

Parameters
[in]funcFunction to run in the thread pool.
[in]callbackCallback function that takes the return value of func as its argument.
[in]arg0Argument to a function run in the thread pool.
[in]arg1Argument to a function run in the thread pool.
[in]arg2Argument to a function run in the thread pool.
[in]arg3Argument to a function run in the thread pool.
[in]arg4Argument to a function run in the thread pool.
[in]arg5Argument to a function run in the thread pool.
[in]arg6Argument to a function run in the thread pool.
[in]arg7Argument to a function run in the thread pool.
[in]arg8Argument to a function run in the thread pool.
[in]arg9Argument to a function run in the thread pool.
Return values
0Success.
EAGAINThe queue is full.

◆ SubmitVarArgs()

template<size_t N>
errno_t nn::nlib::threading::LockFreeThreadPool< N >::SubmitVarArgs ( Func  func,
Callback  callback,
size_t  n,
  ... 
)
noexcept

A task is deployed to the thread pool by specifying the number of arguments of the function.

Parameters
[in]funcFunction to run in the thread pool.
[in]callbackCallback function that takes the return value of func as its argument.
[in]nThe number of arguments passed to the subsequent func.
Return values
0Success.
EINVALIndicates that n is too large.
EAGAINThe queue is full.
Description
A task can be deployed by setting an arbitrary number of arguments. When the number of arguments is fixed to ten or less, the task can be deployed faster by using the Submit() function.
NULL can be set for func and callback. If NULL is set only for func , NULL is passed to the callback function.

Definition at line 371 of file ThreadPool.h.


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