iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages

#include <nrt/Core/Design/ThreadPool.H>

The ThreadPool class is used to run a potentially large set of jobs on a fixed number of threads.

Examples:
tests/test-ThreadPool.C.

Definition at line 48 of file ThreadPool.H.

Public Member Functions

 ThreadPool (size_t const numThreads=4)
 Start a threadpool with the given number of threads.
 
 ~ThreadPool ()
 Detach() all running threads.
 
template<class RETURN_TYPE >
std::future< RETURN_TYPE > pushJob (std::function< RETURN_TYPE()> job)
 Put a new job onto the work queue and get a future for its completion. More...
 
std::future< void > pushJobs (std::vector< std::function< void()> > jobs)
 Put a set of jobs onto the work queue and get a future for their completion. More...
 
void joinAll ()
 Wait for all jobs to finish and resize the thread pool to zero. More...
 
void resize (size_t const numThreads)
 Resize the number of available workers.
 
size_t numWorkers ()
 Get the number of threads in the pool.
 
size_t numJobs ()
 Get the number of jobs left in the queue.
 
bool busy ()
 Returns true if all workers in the pool are currently in use.
 
template<>
std::future< void > pushJob (std::function< void()> job)
 

Member Function Documentation

template<class RETURN_TYPE >
std::future< RETURN_TYPE > nrt::ThreadPool::pushJob ( std::function< RETURN_TYPE()>  job)
inline

Put a new job onto the work queue and get a future for its completion.

Use this method to push a single job onto the work queue. The returned future will be linked to the return value of the function that you push, such that a call to the returned future's get() method will block until your function has finished executing. The future's get() method will then return whatever value your method returned.

Parameters
jobThe function to be pushed onto the work queue
Template Parameters
RETURN_TYPEThe return type of the function being pushed onto the work queue.
Examples:
tests/test-ThreadPool.C.

Definition at line 100 of file ThreadPoolImpl.H.

std::future< void > nrt::ThreadPool::pushJobs ( std::vector< std::function< void()> >  jobs)
inline

Put a set of jobs onto the work queue and get a future for their completion.

Use this method to push a set of jobs onto the queue simultaneously. The return future will block on a call to wait() until all of the functions in the vector have completed. If you need to actually get the return values of the functions you're pushing, then you must use the singular pushJob() method instead.

Parameters
jobsA set of jobs to be executed by the thread pool.
Examples:
tests/test-ThreadPool.C.

Definition at line 119 of file ThreadPoolImpl.H.

void nrt::ThreadPool::joinAll ( )
inline

Wait for all jobs to finish and resize the thread pool to zero.

Must call resize() to reset the thread pool if doing a restart

Definition at line 133 of file ThreadPoolImpl.H.


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