blocxx
|
The ThreadPool class is used to coordinate a group of threads. More...
#include <ThreadPool.hpp>
Public Types | |
enum | PoolType { FIXED_SIZE, DYNAMIC_SIZE, DYNAMIC_SIZE_NO_QUEUE } |
enum | { UNLIMITED_QUEUE_SIZE = 0 } |
enum | EShutdownQueueFlag { E_DISCARD_WORK_IN_QUEUE, E_FINISH_WORK_IN_QUEUE } |
Public Member Functions | |
ThreadPool (PoolType poolType, UInt32 numThreads, UInt32 maxQueueSize, const Logger &logger, const String &poolName="") | |
Constructor. | |
ThreadPool (PoolType poolType, UInt32 numThreads, UInt32 maxQueueSize, const String &poolName="") | |
bool | addWork (const RunnableRef &work) |
Add an RunnableRef for the pool to execute. | |
bool | tryAddWork (const RunnableRef &work) |
Add an RunnableRef for the pool to execute. | |
bool | tryAddWork (const RunnableRef &work, const Timeout &timeout) |
Add an RunnableRef for the pool to execute. | |
void | shutdown (EShutdownQueueFlag finishWorkInQueue=E_FINISH_WORK_IN_QUEUE, const Timeout &timeout=Timeout::infinite) |
Instruct all threads to exit and stop working. | |
void | shutdown (EShutdownQueueFlag finishWorkInQueue, int timeoutSecs) BLOCXX_DEPRECATED |
void | shutdown (EShutdownQueueFlag finishWorkInQueue, const Timeout &shutdownTimeout, const Timeout &definitiveCancelTimeout) |
Instruct all threads to exit and stop working. | |
void | waitForEmptyQueue () |
Wait for the queue to empty out. | |
~ThreadPool () | |
Destructor. | |
ThreadPool (const ThreadPool &x) | |
ThreadPool & | operator= (const ThreadPool &x) |
Private Attributes | |
IntrusiveReference < ThreadPoolImpl > | m_impl |
The ThreadPool class is used to coordinate a group of threads.
There is a queue maintained of work to do. As each thread in the group is available it will get it's next task from the head of the queue. The queue can have a maximum size limit or can be of unlimited size.
This class is freely copyable. All copies reference the same underlying implementation. This class is thread safe.
Definition at line 63 of file ThreadPool.hpp.
anonymous enum |
Definition at line 72 of file ThreadPool.hpp.
Definition at line 125 of file ThreadPool.hpp.
Definition at line 66 of file ThreadPool.hpp.
BLOCXX_NAMESPACE::ThreadPool::ThreadPool | ( | PoolType | poolType, |
UInt32 | numThreads, | ||
UInt32 | maxQueueSize, | ||
const Logger & | logger, | ||
const String & | poolName = "" |
||
) |
Constructor.
poolType | The type of pool, one of the following: FIXED_SIZE - numThreads threads will be created at instantiation time. No threads are created or destroyed until the pool is shutdown. DYNAMIC_SIZE - Threads will be created as work is added. The number of threads will always be less than numThreads. Threads exit when no more work is available in the queue for 1 second. DYNAMIC_SIZE_NO_QUEUE - Threads will be created as work is added. The number of threads will always be less than or equal to numThreads. Threads exit when no more work is available in the queue for 1 second. A queue with numThreads size is used, however addWork() and tryAddWork() will not allow the number of threads plus the number of RunnableRefs in the queue to be greater than numThreads. |
numThreads | The number of threads in the pool. |
maxQueueSize | The upper bound on the size of the queue. UNLIMITED_QUEUE_SIZE (0) means no limit. It is recommended that maxQueueSize is >= numThreads. This parameter is ignored if poolType == DYNAMIC_SIZE_NO_QUEUE. |
logger | used to log messages if non-NULL. |
poolName | incorporated into log messages to identify the pool. |
ThreadPoolException | if the underlying implementation fails. |
Definition at line 773 of file ThreadPool.cpp.
References DYNAMIC_SIZE, DYNAMIC_SIZE_NO_QUEUE, FIXED_SIZE, and m_impl.
BLOCXX_NAMESPACE::ThreadPool::ThreadPool | ( | PoolType | poolType, |
UInt32 | numThreads, | ||
UInt32 | maxQueueSize, | ||
const String & | poolName = "" |
||
) |
Definition at line 756 of file ThreadPool.cpp.
References DYNAMIC_SIZE, DYNAMIC_SIZE_NO_QUEUE, FIXED_SIZE, and m_impl.
BLOCXX_NAMESPACE::ThreadPool::~ThreadPool | ( | ) |
Destructor.
This will call shutdown(E_DISCARD_WORK_IN_QUEUE, 1) when the last copy is destroyed
Definition at line 824 of file ThreadPool.cpp.
BLOCXX_NAMESPACE::ThreadPool::ThreadPool | ( | const ThreadPool & | x | ) |
Definition at line 828 of file ThreadPool.cpp.
bool BLOCXX_NAMESPACE::ThreadPool::addWork | ( | const RunnableRef & | work | ) |
Add an RunnableRef for the pool to execute.
If the queue is full, this call will block until there is space in the queue.
Definition at line 789 of file ThreadPool.cpp.
References BLOCXX_NAMESPACE::Timeout::infinite, and m_impl.
ThreadPool & BLOCXX_NAMESPACE::ThreadPool::operator= | ( | const ThreadPool & | x | ) |
Definition at line 834 of file ThreadPool.cpp.
References m_impl.
void BLOCXX_NAMESPACE::ThreadPool::shutdown | ( | EShutdownQueueFlag | finishWorkInQueue = E_FINISH_WORK_IN_QUEUE , |
const Timeout & | timeout = Timeout::infinite |
||
) |
Instruct all threads to exit and stop working.
After shutdown() is called, addWork() and tryAddWork() will return false.
finishWorkInQueue | If E_FINISH_WORK_IN_QUEUE, threads will continue to process the current work in the queue, before shutting down. If E_DISCARD_WORK_IN_QUEUE, the work in the queue will be discarded. |
timeout | The length of time to wait for the threads to finish their work (and possibly the work in the queue) before attempting to shutdown & cancel the threads. If the timeout is infinite, then the threads will not be cancelled, and shutdown() will return once all the threads have exited (which may never happen if one or more of the threads are deadlocked). |
Definition at line 809 of file ThreadPool.cpp.
References m_impl.
void BLOCXX_NAMESPACE::ThreadPool::shutdown | ( | EShutdownQueueFlag | finishWorkInQueue, |
int | timeoutSecs | ||
) |
Definition at line 804 of file ThreadPool.cpp.
References m_impl, and BLOCXX_NAMESPACE::Timeout::relative().
void BLOCXX_NAMESPACE::ThreadPool::shutdown | ( | EShutdownQueueFlag | finishWorkInQueue, |
const Timeout & | shutdownTimeout, | ||
const Timeout & | definitiveCancelTimeout | ||
) |
Instruct all threads to exit and stop working.
After shutdown() is called, addWork() and tryAddWork() will return false. Both timeouts are measure from the start of the function.
finishWorkInQueue | If E_FINISH_WORK_IN_QUEUE, threads will continue to process the current work in the queue, before shutting down. If E_DISCARD_WORK_IN_QUEUE, the work in the queue will be discarded. |
shutdownTimeout | The length of time to wait for the threads to finish their work (and possibly the work in the queue) before attempting to shutdown the threads. If the timeout is infinite, then the threads will not be shutdown, and shutdown() will return once all the threads have exited (which may never happen if one or more of the threads are deadlocked). |
definitiveCancelTimeout | The length of time to wait for the threads to exit before attempting to definitively cancel the threads. If the timeout is infinite, then the threads will not be cancelled, and shutdown() will return once all the threads have exited (which may never happen if one or more of the threads are deadlocked). |
Definition at line 814 of file ThreadPool.cpp.
References m_impl.
bool BLOCXX_NAMESPACE::ThreadPool::tryAddWork | ( | const RunnableRef & | work | ) |
Add an RunnableRef for the pool to execute.
If the queue is full, this call will *not* block.
Definition at line 794 of file ThreadPool.cpp.
References m_impl, and BLOCXX_NAMESPACE::Timeout::relative().
bool BLOCXX_NAMESPACE::ThreadPool::tryAddWork | ( | const RunnableRef & | work, |
const Timeout & | timeout | ||
) |
Add an RunnableRef for the pool to execute.
If the queue is full, this call will block until the timeout has expired.
Definition at line 799 of file ThreadPool.cpp.
References m_impl.
void BLOCXX_NAMESPACE::ThreadPool::waitForEmptyQueue | ( | ) |
Definition at line 190 of file ThreadPool.hpp.
Referenced by addWork(), operator=(), shutdown(), ThreadPool(), tryAddWork(), and waitForEmptyQueue().