Management of a pool of threads for a specific task. More...
#include <ThreadPool.hpp>
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. | |
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; }
| 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.
| 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.
| int sal3d::ThreadPool< Task >::nthreads | ( | ) | const [inline] |
Return the number of threads in the pool.
| 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.
| 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.
| 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.
1.7.2