library of assembled shared sources |
http://lass.cocamware.com |
#include <thread_pool.h>
Data Structures | |
class | ConsumerThread |
Public Types | |
enum | { autoNumberOfThreads = 0, unlimitedNumberOfTasks = 0 } |
typedef TaskType | TTask |
typedef ConsumerType | TConsumer |
typedef IdlePolicy | TIdlePolicy |
typedef ParticipationPolicy < TaskType, ConsumerType, IdlePolicy > | TParticipationPolicy |
typedef ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy > | TSelf |
Public Member Functions | |
ThreadPool (size_t numberOfThreads=autoNumberOfThreads, size_t maximumNumberOfTasksInQueue=unlimitedNumberOfTasks, const TConsumer &consumerPrototype=TConsumer(), const char *name=0) | |
~ThreadPool () | |
void | addTask (typename util::CallTraits< TTask >::TParam task) |
submit a task to the pool, and block if queue is full. | |
void | completeAllTasks () |
blocks until all tasks in the queue are completed control thread participates as consumer if policy allows for it. | |
void | clearQueue () |
clear queue without completing tasks. | |
const size_t | numberOfThreads () const |
Private Types | |
typedef stde::lock_free_queue < TTask > | TTaskQueue |
Private Member Functions | |
void | startThreads (const TConsumer &consumerPrototype, const char *name) |
Allocate a bunch of threads and run them. | |
void | stopThreads (size_t numAllocatedThreads) |
Deallocate the threads and free memory. | |
Private Attributes | |
TTaskQueue | waitingTasks_ |
std::auto_ptr < experimental::RemoteExceptionBase > | error_ |
ConsumerThread * | threads_ |
unsigned long | mSecsToSleep_ |
size_t | numThreads_ |
size_t | maxWaitingTasks_ |
volatile size_t | numWaitingTasks_ |
volatile size_t | numRunningTasks_ |
volatile bool | shutDown_ |
volatile bool | abort_ |
Friends | |
class | ConsumerThread |
Definition at line 251 of file thread_pool.h.
typedef TaskType lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::TTask |
Definition at line 255 of file thread_pool.h.
typedef ConsumerType lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::TConsumer |
Definition at line 256 of file thread_pool.h.
typedef IdlePolicy lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::TIdlePolicy |
Definition at line 257 of file thread_pool.h.
typedef ParticipationPolicy<TaskType, ConsumerType, IdlePolicy> lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::TParticipationPolicy |
Definition at line 258 of file thread_pool.h.
typedef ThreadPool<TaskType, ConsumerType, IdlePolicy, ParticipationPolicy> lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::TSelf |
Definition at line 259 of file thread_pool.h.
typedef stde::lock_free_queue<TTask> lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::TTaskQueue [private] |
Definition at line 280 of file thread_pool.h.
anonymous enum |
Definition at line 261 of file thread_pool.h.
ThreadPool::ThreadPool | ( | size_t | numberOfThreads = autoNumberOfThreads , |
|
size_t | maximumNumberOfTasksInQueue = unlimitedNumberOfTasks , |
|||
const TConsumer & | consumerPrototype = TConsumer() , |
|||
const char * | name = 0 | |||
) | [inline] |
iNumberOfThreads | specify number of producer threads. Specify autoNumberOfThreads to automatically use as many threads as processors. | |
mNumberOfTasksInQueue | specifiy the maximum number of tasks that may be waiting in the queue. Specify unlimitedNumberOfTasks to have an unlimited queue. |
Definition at line 62 of file thread_pool.inl.
References LASS_ENFORCE, lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::numThreads_, and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::startThreads().
ThreadPool::~ThreadPool | ( | ) | [inline] |
Definition at line 83 of file thread_pool.inl.
References lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::completeAllTasks(), LASS_CATCH_TO_WARNING, lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::numThreads_, and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::stopThreads().
void ThreadPool::addTask | ( | typename util::CallTraits< TTask >::TParam | task | ) | [inline] |
submit a task to the pool, and block if queue is full.
Function waits until tasks can be added to queue without participating as producer (in case of SelfParticipating).
Definition at line 100 of file thread_pool.inl.
References lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::abort_, lass::util::atomicCompareAndSwap(), lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::error_, lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::maxWaitingTasks_, lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::numWaitingTasks_, lass::stde::lock_free_queue< T, FixedAllocator >::push(), lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::unlimitedNumberOfTasks, and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::waitingTasks_.
void ThreadPool::completeAllTasks | ( | ) | [inline] |
blocks until all tasks in the queue are completed control thread participates as consumer if policy allows for it.
Definition at line 130 of file thread_pool.inl.
References lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::abort_, lass::util::atomicDecrement(), lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::error_, lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::numRunningTasks_, lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::numWaitingTasks_, and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::waitingTasks_.
Referenced by lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::~ThreadPool().
void ThreadPool::clearQueue | ( | ) | [inline] |
clear queue without completing tasks.
All waiting tasks in the queue are simply thrown away without ever being completed.
Definition at line 153 of file thread_pool.inl.
References lass::stde::lock_free_queue< T, FixedAllocator >::pop(), and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::waitingTasks_.
const size_t ThreadPool::numberOfThreads | ( | ) | const [inline] |
Definition at line 164 of file thread_pool.inl.
References lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::numThreads_.
void ThreadPool::startThreads | ( | const TConsumer & | consumerPrototype, | |
const char * | name | |||
) | [inline, private] |
Allocate a bunch of threads and run them.
We can't simply use a std::vector or an C-array, because the threads are neither copy-constructable or default constructable. So we need to do our own housekeeping. Don't worry folks, I know what I'm doing ;) [Bramz]
Definition at line 179 of file thread_pool.inl.
References lass::util::Thread::bind(), lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::ConsumerThread, LASS_ASSERT, lass::util::numberOfProcessors, lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::numThreads_, lass::util::Thread::run(), lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::stopThreads(), and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::threads_.
Referenced by lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::ThreadPool().
void ThreadPool::stopThreads | ( | size_t | numAllocatedThreads | ) | [inline, private] |
Deallocate the threads and free memory.
no | exceptions should be throw, nor bubble up from this function ... ever! |
Definition at line 247 of file thread_pool.inl.
References lass::util::Thread::join(), LASS_ASSERT, LASS_CATCH_TO_WARNING, lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::shutDown_, and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::threads_.
Referenced by lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::startThreads(), and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::~ThreadPool().
friend class ConsumerThread [friend] |
Definition at line 282 of file thread_pool.h.
Referenced by lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::startThreads().
TTaskQueue lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::waitingTasks_ [private] |
Definition at line 297 of file thread_pool.h.
Referenced by lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::addTask(), lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::clearQueue(), lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::completeAllTasks(), and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::ConsumerThread::doRun().
std::auto_ptr<experimental::RemoteExceptionBase> lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::error_ [private] |
Definition at line 298 of file thread_pool.h.
Referenced by lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::addTask(), lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::completeAllTasks(), and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::ConsumerThread::doRun().
ConsumerThread* lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::threads_ [private] |
unsigned long lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::mSecsToSleep_ [private] |
Definition at line 300 of file thread_pool.h.
size_t lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::numThreads_ [private] |
Definition at line 301 of file thread_pool.h.
Referenced by lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::numberOfThreads(), lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::startThreads(), lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::ThreadPool(), and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::~ThreadPool().
size_t lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::maxWaitingTasks_ [private] |
Definition at line 302 of file thread_pool.h.
Referenced by lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::addTask().
volatile size_t lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::numWaitingTasks_ [private] |
Definition at line 303 of file thread_pool.h.
Referenced by lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::addTask(), lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::completeAllTasks(), and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::ConsumerThread::doRun().
volatile size_t lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::numRunningTasks_ [private] |
volatile bool lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::shutDown_ [private] |
volatile bool lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::abort_ [private] |
Definition at line 306 of file thread_pool.h.
Referenced by lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::addTask(), lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::completeAllTasks(), and lass::util::ThreadPool< TaskType, ConsumerType, IdlePolicy, ParticipationPolicy >::ConsumerThread::doRun().
Generated on Mon Nov 10 14:22:17 2008 for Library of Assembled Shared Sources by 1.5.7.1 |