LAMA
|
Class that defines a pool of threads that will execute scheduled tasks (see LAMAThreadTask). More...
#include <LAMAThreadPool.hpp>
Public Member Functions | |
LAMAThreadPool (int size) | |
Create a new thread pool with a certain number of threads. | |
boost::shared_ptr< LAMAThreadTask > | schedule (boost::function< void()> work, int numOmpThreads=0) |
Schedules a new function that will be executed by available free thread. | |
void | wait (boost::shared_ptr< LAMAThreadTask > task) |
Wait on completion of a task. | |
~LAMAThreadPool () | |
Destructor shuts down and deletes all threads of the pool. | |
Private Types | |
enum | WorkerState { WORKING, WAITING } |
Private Member Functions | |
void | worker (int id) |
Method that will be run by the worker threads. | |
LAMA_LOG_DECL_STATIC_LOGGER (logger) | |
Private Attributes | |
unsigned int | mTaskId |
int | mMaxSize |
std::vector< boost::thread * > | mThreads |
std::queue< boost::shared_ptr < LAMAThreadTask > > | mTaskQueue |
boost::condition | mNotifyFinished |
boost::condition | mNotifyTask |
boost::mutex | mTaskQueueMutex |
boost::mutex | mNotifyFinishMutex |
WorkerState | mWorkerState |
Class that defines a pool of threads that will execute scheduled tasks (see LAMAThreadTask).
The main advantage of this class is that creation of running threads is only done once. Non-busy threads will wait on notification for new tasks.
Note: tasks will be handled by shared pointers so task data might be freed either here or at its creation.
enum lama::LAMAThreadPool::WorkerState [private] |
lama::LAMAThreadPool::LAMAThreadPool | ( | int | size | ) |
Destructor shuts down and deletes all threads of the pool.
References mNotifyTask, mTaskQueue, mTaskQueueMutex, and mThreads.
lama::LAMAThreadPool::LAMA_LOG_DECL_STATIC_LOGGER | ( | logger | ) | [private] |
boost::shared_ptr< LAMAThreadTask > lama::LAMAThreadPool::schedule | ( | boost::function< void()> | work, |
int | numOmpThreads = 0 |
||
) |
Schedules a new function that will be executed by available free thread.
[in] | work | is function to be executed by available thread |
[in] | numOmpThreads | number of openmp threads the task should use (if numOmpThreads == 0 omp_get_max_threads() is used) |
References lama::LAMAThreadTask::create(), lama::LAMAThreadTask::FINISHED, LAMA_REGION, mNotifyTask, mTaskId, mTaskQueue, mTaskQueueMutex, mThreads, and lama::LAMAThreadTask::QUEUED.
Referenced by lama::Task::Task().
void lama::LAMAThreadPool::wait | ( | boost::shared_ptr< LAMAThreadTask > | task | ) |
Wait on completion of a task.
References lama::LAMAThreadTask::FINISHED, LAMA_ASSERT_ERROR, mNotifyFinished, and mNotifyFinishMutex.
Referenced by lama::Task::synchronize().
void lama::LAMAThreadPool::worker | ( | int | id | ) | [private] |
Method that will be run by the worker threads.
References lama::LAMAThreadTask::FINISHED, mNotifyFinished, mNotifyFinishMutex, mNotifyTask, mTaskQueue, mTaskQueueMutex, mWorkerState, lama::LAMAThreadTask::RUNNING, WAITING, and WORKING.
Referenced by LAMAThreadPool().
int lama::LAMAThreadPool::mMaxSize [private] |
Referenced by LAMAThreadPool().
boost::condition lama::LAMAThreadPool::mNotifyFinished [private] |
boost::mutex lama::LAMAThreadPool::mNotifyFinishMutex [private] |
boost::condition lama::LAMAThreadPool::mNotifyTask [private] |
Referenced by schedule(), worker(), and ~LAMAThreadPool().
unsigned int lama::LAMAThreadPool::mTaskId [private] |
Referenced by LAMAThreadPool(), and schedule().
std::queue<boost::shared_ptr<LAMAThreadTask> > lama::LAMAThreadPool::mTaskQueue [private] |
Referenced by schedule(), worker(), and ~LAMAThreadPool().
boost::mutex lama::LAMAThreadPool::mTaskQueueMutex [private] |
Referenced by schedule(), worker(), and ~LAMAThreadPool().
std::vector<boost::thread*> lama::LAMAThreadPool::mThreads [private] |
Referenced by LAMAThreadPool(), schedule(), and ~LAMAThreadPool().
Referenced by worker().