OW_NAMESPACE::ThreadPool Class Reference

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

#include <OW_ThreadPool.hpp>

Inheritance diagram for OW_NAMESPACE::ThreadPool:

Inheritance graph
[legend]
Collaboration diagram for OW_NAMESPACE::ThreadPool:

Collaboration graph
[legend]
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 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)
ThreadPooloperator= (const ThreadPool &x)

Private Attributes

IntrusiveReference< ThreadPoolImplm_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 59 of file OW_ThreadPool.hpp.


Member Enumeration Documentation

anonymous enum
 

Enumerator:
UNLIMITED_QUEUE_SIZE 

Definition at line 68 of file OW_ThreadPool.hpp.

enum OW_NAMESPACE::ThreadPool::EShutdownQueueFlag
 

Enumerator:
E_DISCARD_WORK_IN_QUEUE 
E_FINISH_WORK_IN_QUEUE 

Definition at line 114 of file OW_ThreadPool.hpp.

enum OW_NAMESPACE::ThreadPool::PoolType
 

Enumerator:
FIXED_SIZE 
DYNAMIC_SIZE 
DYNAMIC_SIZE_NO_QUEUE 

Definition at line 62 of file OW_ThreadPool.hpp.


Constructor & Destructor Documentation

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

Constructor.

Parameters:
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.
Exceptions:
ThreadPoolException if the underlying implementation fails.

Definition at line 627 of file OW_ThreadPool.cpp.

References DYNAMIC_SIZE, DYNAMIC_SIZE_NO_QUEUE, FIXED_SIZE, logger, and m_impl.

OW_NAMESPACE::ThreadPool::~ThreadPool  ) 
 

Destructor.

This will call shutdown(E_DISCARD_WORK_IN_QUEUE, 1) when the last copy is destroyed

Definition at line 668 of file OW_ThreadPool.cpp.

OW_NAMESPACE::ThreadPool::ThreadPool const ThreadPool x  ) 
 

Definition at line 672 of file OW_ThreadPool.cpp.


Member Function Documentation

bool OW_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 648 of file OW_ThreadPool.cpp.

References m_impl.

ThreadPool & OW_NAMESPACE::ThreadPool::operator= const ThreadPool x  ) 
 

Definition at line 678 of file OW_ThreadPool.cpp.

References m_impl.

void OW_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.

Parameters:
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 658 of file OW_ThreadPool.cpp.

References m_impl.

bool OW_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 653 of file OW_ThreadPool.cpp.

References m_impl.

void OW_NAMESPACE::ThreadPool::waitForEmptyQueue  ) 
 

Wait for the queue to empty out.

Definition at line 663 of file OW_ThreadPool.cpp.

References m_impl.


Member Data Documentation

IntrusiveReference<ThreadPoolImpl> OW_NAMESPACE::ThreadPool::m_impl [private]
 

Definition at line 153 of file OW_ThreadPool.hpp.

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


The documentation for this class was generated from the following files:
Generated on Thu Feb 9 09:14:46 2006 for openwbem by  doxygen 1.4.6