#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 LoggerRef &logger=LoggerRef(), const String &poolName="") | |
| Constructor. | |
| 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. | |
| void | shutdown (EShutdownQueueFlag finishWorkInQueue=E_FINISH_WORK_IN_QUEUE, int timeoutSecs=-1) |
| 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 |
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 61 of file ThreadPool.hpp.
Definition at line 64 of file ThreadPool.hpp.
| anonymous enum |
Definition at line 116 of file ThreadPool.hpp.
| BLOCXX_NAMESPACE::ThreadPool::ThreadPool | ( | PoolType | poolType, | |
| UInt32 | numThreads, | |||
| UInt32 | maxQueueSize, | |||
| const LoggerRef & | logger = LoggerRef(), |
|||
| const String & | poolName = "" | |||
| ) |
Constructor.
| poolType | The type of pool, either FIXED_SIZE or DYNAMIC_SIZE 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 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 629 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 670 of file ThreadPool.cpp.
| BLOCXX_NAMESPACE::ThreadPool::ThreadPool | ( | const ThreadPool & | x | ) |
Definition at line 674 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 650 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 655 of file ThreadPool.cpp.
References m_impl.
| void BLOCXX_NAMESPACE::ThreadPool::shutdown | ( | EShutdownQueueFlag | finishWorkInQueue = E_FINISH_WORK_IN_QUEUE, |
|
| int | timeoutSecs = -1 | |||
| ) |
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. | |
| timeoutSecs | The number of seconds to wait for the threads to finish their work (and possibly the work in the queue) before attempting to cancel the threads. If timeoutSecs < 0, the timeout will be unlimited, 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 660 of file ThreadPool.cpp.
References m_impl.
| void BLOCXX_NAMESPACE::ThreadPool::waitForEmptyQueue | ( | ) |
| ThreadPool & BLOCXX_NAMESPACE::ThreadPool::operator= | ( | const ThreadPool & | x | ) |
Definition at line 155 of file ThreadPool.hpp.
Referenced by addWork(), operator=(), shutdown(), ThreadPool(), tryAddWork(), and waitForEmptyQueue().
1.5.3