blocxx
Public Types | Public Member Functions | Private Attributes

BLOCXX_NAMESPACE::ThreadPool Class Reference

The ThreadPool class is used to coordinate a group of threads. More...

#include <ThreadPool.hpp>

Inheritance diagram for BLOCXX_NAMESPACE::ThreadPool:
BLOCXX_NAMESPACE::IntrusiveCountableBase

List of all members.

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)
ThreadPooloperator= (const ThreadPool &x)

Private Attributes

IntrusiveReference
< ThreadPoolImpl
m_impl

Detailed Description

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.


Member Enumeration Documentation

anonymous enum
Enumerator:
UNLIMITED_QUEUE_SIZE 

Definition at line 72 of file ThreadPool.hpp.

Enumerator:
E_DISCARD_WORK_IN_QUEUE 
E_FINISH_WORK_IN_QUEUE 

Definition at line 125 of file ThreadPool.hpp.

Enumerator:
FIXED_SIZE 
DYNAMIC_SIZE 
DYNAMIC_SIZE_NO_QUEUE 

Definition at line 66 of file ThreadPool.hpp.


Constructor & Destructor Documentation

BLOCXX_NAMESPACE::ThreadPool::ThreadPool ( PoolType  poolType,
UInt32  numThreads,
UInt32  maxQueueSize,
const Logger logger,
const String poolName = "" 
)

Constructor.

Parameters:
poolTypeThe 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.
numThreadsThe number of threads in the pool.
maxQueueSizeThe 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.
loggerused to log messages if non-NULL.
poolNameincorporated into log messages to identify the pool.
Exceptions:
ThreadPoolExceptionif 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.


Member Function Documentation

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.

Returns:
true if added to the queue, false if not, which will only happen if the pool is shutting down.

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.

Parameters:
finishWorkInQueueIf 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.
timeoutThe 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.

Parameters:
finishWorkInQueueIf 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.
shutdownTimeoutThe 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).
definitiveCancelTimeoutThe 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.

Returns:
true if added to the queue, false if not.

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.

Returns:
true if added to the queue, false if not.

Definition at line 799 of file ThreadPool.cpp.

References m_impl.

void BLOCXX_NAMESPACE::ThreadPool::waitForEmptyQueue ( )

Wait for the queue to empty out.

Definition at line 819 of file ThreadPool.cpp.

References m_impl.


Member Data Documentation

Definition at line 190 of file ThreadPool.hpp.

Referenced by addWork(), operator=(), shutdown(), ThreadPool(), tryAddWork(), and waitForEmptyQueue().


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