A Hopf, Skip and a Jump
|
A utility class to permit a single function to be run on multiple datasets concurrently. More...
#include <thread_pool.h>
Public Types | |
typedef std::function< void(void *)> | delegate_t |
The type of the delegate function each thread will run. | |
Public Member Functions | |
ThreadPool (std::size_t numThreads=0) | |
Construct a thread pool. | |
~ThreadPool () | |
Destroy the thread pool. More... | |
void | manifold (delegate_t delegate, void **params, std::size_t jobs) |
Execute a function in each of the threads, passing each one its own paramater set. More... | |
Public Attributes | |
const std::size_t | threads |
Number of threads in pool. | |
Protected Types | |
enum | state { waiting, running, dying, dead } |
thread state descriptor | |
Protected Member Functions | |
void | wait_raise (std::unique_lock< std::mutex > &lk, const std::size_t to_check) |
Convenience function: waits for a lock then deals with any exceptions propagated from the threads. More... | |
void | dispatcher (int id) |
Dispatch jobs to the given delegate. More... | |
Protected Attributes | |
std::unique_ptr< std::thread[]> | workers |
Worker threads. | |
std::unique_ptr< enum state[]> | states |
State variables for each thread. | |
std::unique_ptr< std::exception_ptr[]> | exceptions |
exception_ptrs for each thread | |
void ** | params |
Parameters for each thread, passed to the runJobs function. | |
delegate_t | delegate |
The work the workers are supposed to perform. | |
std::mutex | m |
Mutex to control access to job packets. | |
std::condition_variable | cv |
Condition variable through which to notify workers that jobs are available. | |
std::size_t | remain |
Count of the number of workers still to complete their job packets. | |
A utility class to permit a single function to be run on multiple datasets concurrently.
The class may be instanced giving the number of concurrent threads to run, or by requesting 0 threads (the default value), a number will be chosen equal to the inherent concurrency of the platform on which the application will run.
The caller must prepare an array of parameter blocks ("jobs"). These along with the delegate function are passed to the manifold() method, and each concurrent invocation of the delegate function will be passed a separate parameter block.
The number of jobs must be defined in the manifold() call but need not be equal to the number of threads. If the number of jobs exceeds the number of threads, work will be performed in batches. The size of each batch is the number of threads given when the ThreadPool object is created. manifold() does not return until all jobs are complete.
Threads are reused across calls to manifold(). When the ThreadPool is destroyed, it closes down all threads and awaits their proper termination.
Definition at line 36 of file thread_pool.h.
ThreadPool::~ThreadPool | ( | ) |
Destroy the thread pool.
Waits for all executive threads to terminate.
|
protected |
Dispatch jobs to the given delegate.
id | index into the params/states arrays to be used by this thread |
void ThreadPool::manifold | ( | delegate_t | delegate, |
void ** | params, | ||
std::size_t | jobs | ||
) |
Execute a function in each of the threads, passing each one its own paramater set.
The number of jobs (== the number of paramater sets) is passed in the final argument. If this exceeds the number of threads in the pool, the jobs will be sequenced into batches. mainfold() will only return when all have been completed.
delegate | The function each thread should call. |
params | An array of pointers to parameters to pass. |
jobs | Number of threads to run. |
|
protected |
Convenience function: waits for a lock then deals with any exceptions propagated from the threads.
lk | The lock on which to wait |
to_check | Number of threads running on this lock |