Public Member Functions

sal3d::ThreadPool< Task > Class Template Reference
[Acquire]

Management of a pool of threads for a specific task. More...

#include <ThreadPool.hpp>

List of all members.

Public Member Functions

 ThreadPool (int nthreads)
 Start a ThreadPool with the given number of threads.
 ~ThreadPool ()
 Destroy the ThreadPool, deleting its related threads.
void post (const Task &t)
 Give the task to one free thread to run it.
void wait ()
 Wait for all the tasks given to threads to finish.
bool wait (int timeout_ms)
 Wait with timeout for all the tasks given to threads to finish.
int nthreads () const
 Return the number of threads in the pool.

Detailed Description

template<typename Task>
class sal3d::ThreadPool< Task >

Management of a pool of threads for a specific task.

This class requires linking with acquire.lib (import library for acquire.dll).

At the ThreadPool creation, the given number of threads will be spawned ready to execute the tasks. The ThreadPool object allows queuing tasks, which will be run by any free thread in the pool, maintaining the threads busy as much as possible.

The tasks are queued through the post() method, and it is possible to wait until all the queued tasks have been run using the wait() method.

The order of the tasks being run is not guaranteed to be that of the enqueue time, and the any tasks queued may be run in parallel. If you need some guarantee of calling order, please consider the PipeLine class.

The task objects passed to post() should be copiable, because the threads will call the Task::operator() of a heap copy of the task object passed to post().

ThreadPool objects cannot be copied, in order to maintain a single entry point for task queuing and waiting, for any given ThreadPool.

Example of usage, capturing 1000 frames with the software PeakFinder using four threads, for a four core computer:

 sal3d::RangeMap
 capture1000Profiles(sal3d::FrameGrabber &fg)
 {
     class pftask
     {
         public:
             pftask(const sal3d::Frame &f,
                 sal3d::Profile &p,
                 const sal3d::PeakFinder &pf)
                 :_frame(f), _profile(p), _peakfinder(pf)
             {
             }

             void operator()()
             {
                 _peakfinder(_frame, _profile);
             }

         private:
             const sal3d::Frame _frame;
             sal3d::Profile _profile;
             const sal3d::PeakFinder &_peakfinder;
     };

     const int nframes = 1000;
     ThreadPool<pftask> pool(4); // Four threads
     std::auto_ptr<sal3d::RangeMap> rm;
     sal3d::PeakFinder pf(100 /* threshold */);
 
     for(int i = 0; i < nframes; ++i)
     {
         sal3d::Frame f(fg.frame());
         // Once we have captured one frame, create a rangemap with
         // the needed profile length.
         if (i == 0)
             rm.reset(new sal3d::RangeMap(f.height(), nframes));
         sal3d::Profile profile(rm->getNewProfile());
         pftask task(f, profile, pf);
         pool.post(task);
     }
     pool.wait();
     return *rm;
 }

Constructor & Destructor Documentation

template<typename Task >
sal3d::ThreadPool< Task >::ThreadPool ( int  nthreads ) [inline]

Start a ThreadPool with the given number of threads.

This will spawn as threads as the user requested, and they will wait for tasks to be done. These threads will last until the ThreadPool object is destroyed.

template<typename Task >
sal3d::ThreadPool< Task >::~ThreadPool (  ) [inline]

Destroy the ThreadPool, deleting its related threads.

This will wait for all the thread tasks to be finished, and then it will destroy the threads managed by this object.


Member Function Documentation

template<typename Task >
int sal3d::ThreadPool< Task >::nthreads (  ) const [inline]

Return the number of threads in the pool.

Returns:
The number of threads in the pool, set at constructor time
template<typename Task >
void sal3d::ThreadPool< Task >::post ( const Task &  t ) [inline]

Give the task to one free thread to run it.

This method will block until there one thread free in the pool, and then it will give a copy of the task object passed to that thread. The thread will call the Task::operator()() method of the given task object copy. This method will return once the task is given to the thread, regardless of the time when the task finishes.

template<typename Task >
bool sal3d::ThreadPool< Task >::wait ( int  timeout_ms ) [inline]

Wait with timeout for all the tasks given to threads to finish.

Block until all the tasks ever given to the threads in this ThreadPool are finished, or until the timeout finishes.

Returns:
true when all tasks finished before the timeout, false otherwise
template<typename Task >
void sal3d::ThreadPool< Task >::wait (  ) [inline]

Wait for all the tasks given to threads to finish.

Block until all the tasks ever given to the threads in this ThreadPool are finished.


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