This is a concrete class that provides for a basic thread pool. More...
#include <ThreadPool.h>
Classes | |
| class | condition_variable |
| class | WorkItemRet |
| Class to define a work item returning a variable. More... | |
Public Types | |
| typedef ThreadPoolWorkItem | WorkItem |
| Base class for the work item (users should derive from WorkItemRet) | |
Public Member Functions | |
| std::vector< ThreadPoolID > | add_work (const std::vector< ThreadPool::WorkItem * > &work, const std::vector< int > &priority=std::vector< int >()) |
| Function to add multiple work items. | |
| ThreadPoolID | add_work (ThreadPool::WorkItem *work, int priority=0) |
| Function to add a work item. | |
| int | getNumThreads () const |
| Function to return the number of threads in the thread pool. | |
| std::vector< int > | getThreadAffinity (int thread) const |
| int | getThreadNumber () const |
| Function to return the current thread number. | |
| bool | isFinished (const ThreadPoolID &id) const |
| Function to check if the work item has finished processing. | |
| bool | isValid (const ThreadPoolID &id) const |
| Function to check if the work item is valid. | |
| int | N_queued () const |
| Return the number of items queued. | |
| ThreadPool & | operator= (const ThreadPool &)=delete |
| Assignment operator. | |
| void | setErrorHandler (std::function< void(const std::string &)> fun) |
| Set the error handler for threads. | |
| void | setMaxWaitTimeDebug (const int time) |
| Function to set the maximum wait time. | |
| void | setNumThreads (const int N, const std::string &affinity="none", const std::vector< int > &procs=std::vector< int >()) |
| Function to set the number of threads in the thread pool. | |
| void | setThreadAffinity (int thread, const std::vector< int > &procs) const |
| ThreadPool (const int N=0, const std::string &affinity="none", const std::vector< int > &procs=std::vector< int >(), int queueSize=4096) | |
| ThreadPool (const ThreadPool &)=delete | |
| Copy constructor. | |
| void | wait (const ThreadPoolID &id) const |
| Function to wait until a specific work item has finished. | |
| void | wait_all (const std::vector< ThreadPoolID > &ids) const |
| Function to wait until all of the given work items have finished their work. | |
| size_t | wait_any (const std::vector< ThreadPoolID > &ids) const |
| Function to wait until any of the given work items have finished their work. | |
| void | wait_pool_finished () const |
| Function to wait until all work items in the thread pool have finished their work. | |
| std::vector< int > | wait_some (int N_wait, const std::vector< ThreadPoolID > &ids, int max_wait=10000000) const |
| Function to wait until some of the given work items have finished their work. | |
| ~ThreadPool () | |
| Destructor. | |
Static Public Member Functions | |
| static std::vector< ThreadPoolID > | add_work (ThreadPool *tpool, const std::vector< ThreadPool::WorkItem * > &work, const std::vector< int > &priority=std::vector< int >()) |
| Function to add multiple work items. | |
| static ThreadPoolID | add_work (ThreadPool *tpool, ThreadPool::WorkItem *work, int priority=0) |
| Function to add a work item. | |
| template<class Ret , class... Args1, class... Args2> | |
| static WorkItem * | createWork (Ret(*routine)(Args1...), Args2... args) |
| Function to create a work item. | |
| template<class Ret , class... Args1, class... Args2> | |
| static WorkItem * | createWork (Ret(*routine)(Args1...), std::tuple< Args2... > &&args) |
| Function to create a work item. | |
| template<class Ret , class... Args1, class... Args2> | |
| static WorkItem * | createWork (std::function< Ret(Args1...)> routine, Args2... args) |
| Function to create a work item. | |
| template<class Ret , class... Args1, class... Args2> | |
| static WorkItem * | createWork (std::function< Ret(Args1...)> routine, std::tuple< Args2... > &&args) |
| Function to create a work item. | |
| static int | getCurrentProcessor () |
| Function to return the processor number that the current thread is running on. | |
| template<class return_type > | |
| static return_type | getFunctionRet (const ThreadPoolID &id) |
| Function to get the returned function value. | |
| static int | getNumberOfProcessors () |
| Function to return the number of processors available. | |
| static std::vector< int > | getProcessAffinity () |
| Function to return the affinity of the current process. | |
| static std::vector< int > | getThreadAffinity () |
| Function to return the affinity of the current thread. | |
| static bool | is_valid (const ThreadPool *tpool) |
| Function to check if the thread pool is valid. | |
| static int | numThreads (const ThreadPool *tpool) |
| Function to return the number of work threads. | |
| static void | set_OS_warnings (int behavior=0) |
| Function to enable/disable OS warning messages. | |
| static void | setProcessAffinity (const std::vector< int > &procs) |
| Function to set the affinity of the current process. | |
| static void | setThreadAffinity (const std::vector< int > &procs) |
| static void | wait_all (const ThreadPool *tpool, const std::vector< ThreadPoolID > &ids) |
| Function to wait until all of the given work items have finished their work. | |
| static void | wait_pool_finished (const ThreadPool *tpool) |
| Function to wait until all work items in the thread pool have finished their work. | |
Static Public Attributes | |
| static constexpr uint16_t | MAX_THREADS = 128 |
Private Types | |
| typedef condition_variable | cond_t |
| typedef volatile std::atomic_uint32_t | vint32_t |
| typedef volatile std::atomic_uint64_t | vint64_t |
Private Member Functions | |
| void | add_work (size_t N, ThreadPool::WorkItem *work[], const int *priority, ThreadPoolID *id) |
| void | check_startup () |
| bool | isMemberThread () const |
| void | print_wait_warning () const |
| void | tpool_thread (int id) |
| void | wait_N (int N, double time) const |
| std::vector< bool > | wait_some (size_t N_work, const ThreadPoolID *ids, size_t N_wait, int max_wait) const |
Static Private Member Functions | |
| static void | create_new_thread (ThreadPool *tpool, int id) |
| static WorkItem * | getFinishedWorkItem (const ThreadPoolID &id) |
Private Attributes | |
| vint64_t | d_active [MAX_THREADS/64] |
| vint64_t | d_cancel [MAX_THREADS/64] |
| std::function< void(const std::string &)> | d_errorHandler |
| vint64_t | d_id_assign |
| int | d_max_wait_time |
| vint64_t | d_N_added |
| vint64_t | d_N_finished |
| vint64_t | d_N_started |
| uint16_t | d_N_threads |
| uint32_t | d_NULL_HEAD |
| uint32_t | d_NULL_TAIL |
| vint32_t | d_num_active |
| ThreadPoolListQueue | d_queue |
| vint32_t | d_signal_count |
| volatile bool | d_signal_empty |
| std::thread * | d_thread |
| cond_t | d_wait_finished |
| cond_t | d_wait_work |
This is a concrete class that provides for a basic thread pool.
This class implements a basic thread pool that can be used for a wide variety of applications. An example call usage is provided below. The ability to return a value is provided. Note that there is a small overhead to using this functionality.
Example:
Existing function call: double x = myfun_1(a,b); double y = myfun_2(c,d);
Threaded call (processing in parallel): ThreadPoolID ids[2]; ids[0] = TPOOL_ADD_WORK( tpool, myfun_1, (a,b) ); ids[1] = TPOOL_ADD_WORK( tpool, myfun_2, (c,d) ); wait_all(2,ids); double x = getFunctionRet(ids[0]); double y = getFunctionRet(ids[1]);
Definition at line 48 of file ThreadPool.h.
|
private |
Definition at line 516 of file ThreadPool.h.
|
private |
Definition at line 514 of file ThreadPool.h.
|
private |
Definition at line 515 of file ThreadPool.h.
Base class for the work item (users should derive from WorkItemRet)
Definition at line 59 of file ThreadPool.h.
| AMP::ThreadPool::ThreadPool | ( | const int | N = 0, |
| const std::string & | affinity = "none", |
||
| const std::vector< int > & | procs = std::vector< int >(), |
||
| int | queueSize = 4096 |
||
| ) |
Constructor that initialize the thread pool with N threads
| N | The desired number of worker threads |
| affinity | The affinity scheduler to use: none - Let the OS handle the affinities (default) independent - Give each thread an independent set of processors |
| procs | The processors to use (defaults to the process affinitiy list) |
| queueSize | The maximum number of items in the queue before forcing a wait |
|
delete |
Copy constructor.
| AMP::ThreadPool::~ThreadPool | ( | ) |
Destructor.
|
inline |
Function to add multiple work items.
This function adds multiple work item to the queue Note: any thread may call this routine.
| work | Vector of pointers to the work items to add Note that the threadpool will automatically destroy the item when finished |
| priority | Vector of values indicating the priority of the work items |
|
private |
|
inlinestatic |
Function to add multiple work items.
This function adds multiple work item to the queue Note: any thread may call this routine.
| tpool | Threadpool to add work to (may be null) |
| work | Vector of pointers to the work items to add Note that the threadpool will automatically destroy the item when finished |
| priority | Vector of values indicating the priority of the work items |
|
inlinestatic |
Function to add a work item.
This function adds a work item to the queue Note: any thread may call this routine.
| tpool | Threadpool to add work to (may be null) |
| work | Pointer to the work item to add Note that the threadpool will automatically destroy the item when finished |
| priority | A value indicating the priority of the work item (0-default) |
|
inline |
Function to add a work item.
This function adds a work item to the queue Note: any thread may call this routine.
| work | Pointer to the work item to add Note that the threadpool will automatically destroy the item when finished |
| priority | A value indicating the priority of the work item (0-default) |
|
private |
|
inlinestaticprivate |
Definition at line 487 of file ThreadPool.h.
References tpool_thread().
|
inlinestatic |
Function to create a work item.
This function creates a work item that can be added to the queue
| routine | Function to call from the thread pool |
| args | Function arguments to pass |
|
inlinestatic |
Function to create a work item.
This function creates a work item that can be added to the queue
| routine | Function to call from the thread pool |
| args | Function arguments to pass |
|
inlinestatic |
Function to create a work item.
This function creates a work item that can be added to the queue
| routine | Function to call from the thread pool |
| args | Function arguments to pass |
|
inlinestatic |
Function to create a work item.
This function creates a work item that can be added to the queue
| routine | Function to call from the thread pool |
| args | Function arguments to pass |
|
static |
Function to return the processor number that the current thread is running on.
|
inlinestaticprivate |
Definition at line 481 of file ThreadPool.h.
|
inlinestatic |
Function to get the returned function value.
This is the function returns the value that was returned from the working function. If the work item has not finished or was not found it will return 0.
| id | The id of the work item |
|
static |
Function to return the number of processors available.
|
inline |
Function to return the number of threads in the thread pool.
Definition at line 162 of file ThreadPool.h.
References d_N_threads.
Referenced by numThreads().
|
static |
Function to return the affinity of the current process.
|
static |
Function to return the affinity of the current thread.
| std::vector< int > AMP::ThreadPool::getThreadAffinity | ( | int | thread | ) | const |
Function to return the affinity of the given thread
| thread | The index of the thread |
| int AMP::ThreadPool::getThreadNumber | ( | ) | const |
Function to return the current thread number.
This function will return the thread number of current active thread. If the thread is not a member of the thread pool, this function will return 0.
Referenced by isMemberThread().
|
static |
Function to check if the thread pool is valid.
Sometimes it is necessary to work with raw pointers for the thread pool. If the thread pool is invalid and used, the program will likely fail catastrophically. This function checks if the thread pool is valid is a relatively safe manner. If the thread pool is pointing to an invalid memory address, because it has been freed, never allocated, or otherwise corrupted, this function will return false.
| tpool | Pointer to the ThreadPool to check |
|
inline |
Function to check if the work item has finished processing.
This function checks if the work item has finished processing.
| id | The id of the work item |
Definition at line 215 of file ThreadPool.h.
|
inlineprivate |
Definition at line 498 of file ThreadPool.h.
References getThreadNumber().
|
inline |
Function to check if the work item is valid.
This function checks if the work item has a valid id. Note: this function does not require blocking and will return immediately.
| id | The id of the work item |
|
inline |
Return the number of items queued.
Definition at line 377 of file ThreadPool.h.
References d_queue, and AMP::ThreadPoolListQueue::size().
|
inlinestatic |
Function to return the number of work threads.
This function returns the number of threads in the thread pool, or 0 if the thread pool is empty or does not exist
| tpool | Threadpool to add work to (may be null) |
Definition at line 392 of file ThreadPool.h.
References getNumThreads().
|
delete |
Assignment operator.
|
private |
|
static |
Function to enable/disable OS warning messages.
Some of the functions such as setting/getting the thread affinities are not supported on all platforms. This function controls the behavior of these functions on systems where they are not supported. The default
behavior is to print a warning message. Other options include ignoring the messages (the functions will return empty sets), or throwing an exception. Note: this is a global property and will affect all thread pools in an application.
| behavior | The behavior of OS specific messages/errors 0: Print a warning message 1: Ignore the messages 2: Throw an error |
| void AMP::ThreadPool::setErrorHandler | ( | std::function< void(const std::string &)> | fun | ) |
Set the error handler for threads.
|
inline |
Function to set the maximum wait time.
This function sets the maximum time the thread pool will wait before warning about a possible hung thread. Default is to wait 10 minutes.
| time | The number of seconds to wait (seconds) |
Definition at line 190 of file ThreadPool.h.
References d_max_wait_time, and AMP::time.
| void AMP::ThreadPool::setNumThreads | ( | const int | N, |
| const std::string & | affinity = "none", |
||
| const std::vector< int > & | procs = std::vector< int >() |
||
| ) |
Function to set the number of threads in the thread pool.
This function will change the number of worker threads in the ThreadPool to the number specified. This function will immediately change the number of threads in the ThreadPool without checking the existing work unless the desired number of threads is 0. In this case, the function will wait for all work items to finish before deleting the existing work threads. Member threads may not call this function.
| N | The desired number of worker threads |
| affinity | The affinity scheduler to use: none - Let the OS handle the affinities (default) independent - Give each thread an independent set of processors |
| procs | The processors to use (defaults to the process affinitiy list) |
|
static |
Function to set the affinity of the current process.
|
static |
Function to set the affinity of the current thread
| procs | The processors to use |
| void AMP::ThreadPool::setThreadAffinity | ( | int | thread, |
| const std::vector< int > & | procs | ||
| ) | const |
Set the given thread to have the specified affinity
| thread | The index of the thread |
| procs | The processors to use |
|
private |
Referenced by create_new_thread().
|
inline |
Function to wait until a specific work item has finished.
This is the function waits for a specific work item to finished. Note: any thread may call this routine, but they will block until finished. For worker threads this may eventually lead to a deadlock.
| id | The work item to wait for |
|
inline |
Function to wait until all of the given work items have finished their work.
This is the function waits for all given of the work items to finish. Note: any thread may call this routine, but they will block until finished. For worker threads this may eventually lead to a deadlock.
| ids | Vector of work items to wait for |
|
inlinestatic |
Function to wait until all of the given work items have finished their work.
This is the function waits for all given of the work items to finish. Note: any thread may call this routine, but they will block until finished. For worker threads this may eventually lead to a deadlock.
| tpool | Threadpool containing work (must match call to add_work) |
| ids | Vector of work items to wait for |
|
inline |
Function to wait until any of the given work items have finished their work.
This is the function waits for any of the given work items to finish. If successful it returns the index of a finished work item (the index in the array ids). Note: any thread may call this routine, but they will block until finished. For worker threads this may eventually lead to a deadlock.
| ids | Vector of work items to wait for |
|
private |
| void AMP::ThreadPool::wait_pool_finished | ( | ) | const |
Function to wait until all work items in the thread pool have finished their work.
This function will wait until all work has finished. Note: member threads may not call this function. Only one non-member thread should call this routine at a time.
Referenced by wait_pool_finished().
|
inlinestatic |
Function to wait until all work items in the thread pool have finished their work.
This function will wait until all work has finished. Note: member threads may not call this function. Only one non-member thread should call this routine at a time.
| tpool | Threadpool containing work (must match call to add_work) |
Definition at line 437 of file ThreadPool.h.
References wait_pool_finished().
|
inline |
Function to wait until some of the given work items have finished their work.
This is the function waits for some of the given work items to finish. If successful it returns the indicies of the finished work items (the index in the array ids). Note: any thread may call this routine, but they will block until finished. For worker threads this may eventually lead to a deadlock.
| N_wait | Number of work items to wait for |
| ids | Vector of work items to wait for |
| max_wait | Maximum time to wait (seconds) |
|
private |
|
private |
Definition at line 526 of file ThreadPool.h.
|
private |
Definition at line 527 of file ThreadPool.h.
|
private |
Definition at line 534 of file ThreadPool.h.
|
private |
Definition at line 525 of file ThreadPool.h.
|
private |
Definition at line 522 of file ThreadPool.h.
Referenced by setMaxWaitTimeDebug().
|
private |
Definition at line 528 of file ThreadPool.h.
|
private |
Definition at line 530 of file ThreadPool.h.
|
private |
Definition at line 529 of file ThreadPool.h.
|
private |
Definition at line 521 of file ThreadPool.h.
Referenced by getNumThreads().
|
private |
Definition at line 519 of file ThreadPool.h.
|
private |
Definition at line 536 of file ThreadPool.h.
|
private |
Definition at line 524 of file ThreadPool.h.
|
private |
Definition at line 533 of file ThreadPool.h.
Referenced by N_queued().
|
mutableprivate |
Definition at line 523 of file ThreadPool.h.
|
mutableprivate |
Definition at line 520 of file ThreadPool.h.
|
private |
Definition at line 535 of file ThreadPool.h.
|
mutableprivate |
Definition at line 531 of file ThreadPool.h.
|
mutableprivate |
Definition at line 532 of file ThreadPool.h.
|
staticconstexpr |
Definition at line 52 of file ThreadPool.h.
|
Advanced Multi-Physics (AMP) Oak Ridge National Laboratory Idaho National Laboratory Los Alamos National Laboratory |
This page automatically produced from the source code by Last updated: Tue Mar 10 2026 13:06:42. Comments on this page |