LAMA
lama::LAMAThreadPool Class Reference

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< LAMAThreadTaskschedule (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

Detailed Description

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.


Member Enumeration Documentation

Enumerator:
WORKING 
WAITING 

Constructor & Destructor Documentation

Create a new thread pool with a certain number of threads.

References mMaxSize, mTaskId, mThreads, and worker().

Destructor shuts down and deletes all threads of the pool.

References mNotifyTask, mTaskQueue, mTaskQueueMutex, and mThreads.


Member Function Documentation

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.

Parameters:
[in]workis function to be executed by available thread
[in]numOmpThreadsnumber of openmp threads the task should use (if numOmpThreads == 0 omp_get_max_threads() is used)
Returns:
shared pointer for the task

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().


Field Documentation

Referenced by LAMAThreadPool().

boost::condition lama::LAMAThreadPool::mNotifyFinished [private]

Referenced by wait(), and worker().

Referenced by wait(), and worker().

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]

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