rwlogo
SourcePro C++ 12.0

SourcePro® C++ API Reference Guide



   SourcePro C++
Documentation Home

RWThreadPool Class Reference
[Threading]

Manages a pool of RWThread instances used to execute work encapsulated as RWFunctor0 functors. More...

#include <rw/thread/RWThreadPool.h>

Inheritance diagram for RWThreadPool:
RWHandleBase

List of all members.

Public Member Functions

 RWThreadPool ()
 RWThreadPool (const RWThreadPool &second)
 ~RWThreadPool ()
RWThreadPooloperator= (const RWThreadPool &second)
void enqueue (const RWFunctor0 &functor)
size_t entries () const
RWThreadAttribute getPoolAttribute ()
bool resize (size_t minThreads, size_t maxThreads=RW_THR_NO_DYNAMIC_THREAD_POOL)
size_t size () const
void stop ()

Static Public Member Functions

static RWThreadPool make (size_t minThreads, size_t maxThreads=RW_THR_NO_DYNAMIC_THREAD_POOL, unsigned long timeout=RW_THR_NO_TIMEOUT)
static RWThreadPool make (size_t minThreads, const RWThreadAttribute &poolThreadsAttr, size_t maxThreads=RW_THR_NO_DYNAMIC_THREAD_POOL, unsigned long timeout=RW_THR_NO_TIMEOUT)

Protected Member Functions

RWThreadPoolImp & body (void) const

Related Functions

(Note that these are not member functions.)



const size_t RW_THR_NO_DYNAMIC_THREAD_POOL
const unsigned long RW_THR_NO_TIMEOUT

Detailed Description

The RWThreadPool object manages a pool of RWThread instances that are used to execute work encapsulated as RWFunctor0 functors. A thread pool object, when started, waits for other threads to enqueue work functors for execution.

Pool threads dequeue functors and execute them to completion. This process continues until the thread pool object passes out of scope, its destructor is called, or the stop() member function is called.

A thread pool can have a fixed number of threads, or its size may grow and shrink dynamically, according to load. The dynamic nature of the pool is controlled by parameters in the make() member function.

Examples

 #include <iostream.h>
 #include <rw/sync/RWMutexLock.h>
 #include <rw/functor/functor0.h>
 #include <rw/thread/RWThreadPool.h>
 
 RWMutexLock coutLock;
 
 void work()
 {
   RWMutexLock::LockGuard guard(coutLock);
   cout << "Hi-ho, hi-ho, it's off to work we go" << endl;
 }
 
 void play()
 {
   RWMutexLock::LockGuard guard(coutLock);
   cout << "Catch a wave and you're sittin' on top of the world"
        << endl;
 }
 
 int main() {
   RWThreadPool thrPool = RWThreadPool::make(5);
 
   // Create a functor using templates:
   RWFunctor0 workFunc = rwtMakeFunctor0((void(*)(void))
                                          NULL, work);
 
   // Create a functor using macros:
   RWFunctor0 playFunc = rwtMakeFunctor0G(void, play);
 
   thrPool.enqueue(workFunc);
   thrPool.enqueue(playFunc);
 
   // stop after all enqueued functors complete:
   thrPool.stop();
   return 0;
 };
See also:
RWThread, RWFunctor0, RWHandleBase::isValid()

Constructor & Destructor Documentation

RWThreadPool::RWThreadPool (  )  [inline]

Constructs an empty (invalid) handle.

RWThreadPool::RWThreadPool ( const RWThreadPool second  )  [inline]

Constructs a new external interface handle to the thread pool object that is pointed to by a second handle (if any).

RWThreadPool::~RWThreadPool (  )  [inline]

Destructor.


Member Function Documentation

RWThreadPoolImp& RWThreadPool::body ( void   )  const [protected]

Gets a reference for the body instance, if any.

Exceptions:
RWTHRInvalidPointer Thrown if this handle is not attached to a body.

Reimplemented from RWHandleBase.

void RWThreadPool::enqueue ( const RWFunctor0 functor  ) 

Enqueues a piece of work in the functor onto the thread pool. All functors must be valid; that is, functor.isValid() must return true. All exceptions that might be thrown within functor must be handled within functor.

Invalid functors and exceptions thrown by functors but not caught by functors are ignored in the release version of the compiled source code. During initial development, therefore, you should compile source code in debug mode, which throws assertions when invalid functors are found or uncaught exceptions are thrown.

size_t RWThreadPool::entries (  )  const

Returns the number of work entries queued in the thread pool.

RWThreadAttribute RWThreadPool::getPoolAttribute (  ) 

Gets a handle to the thread attribute instance specified during thread pool construction.

static RWThreadPool RWThreadPool::make ( size_t  minThreads,
const RWThreadAttribute poolThreadsAttr,
size_t  maxThreads = RW_THR_NO_DYNAMIC_THREAD_POOL,
unsigned long  timeout = RW_THR_NO_TIMEOUT 
) [static]

Makes a thread pool instance that contains a number of threads from minThreads to maxThreads, each with the specified thread attributes. The actual number of threads grows and shrinks between minThreads and maxThreads, depending on the work load. A thread awaits work for at least timeout milliseconds before exiting.

You are responsible for ensuring the suitability of thread pool attributes. For example, a poolThreadsAttri start policy of RW_THR_START_INTERRUPTED would cause RWThreadPool to hang because all of the threads in the thread pool would be waiting for RWRunnable::releaseInterrupt() calls.

static RWThreadPool RWThreadPool::make ( size_t  minThreads,
size_t  maxThreads = RW_THR_NO_DYNAMIC_THREAD_POOL,
unsigned long  timeout = RW_THR_NO_TIMEOUT 
) [static]

Makes a thread pool instance that contains a number of threads from minThreads to maxThreads, each with the default thread attributes. The actual number of threads grows and shrinks between minThreads to maxThreads depending on work load. A thread awaits work for at least timeout milliseconds before exiting.

RWThreadPool & RWThreadPool::operator= ( const RWThreadPool second  )  [inline]

Binds this external interface handle to the thread pool object that is pointed to by a second handle (if any).

bool RWThreadPool::resize ( size_t  minThreads,
size_t  maxThreads = RW_THR_NO_DYNAMIC_THREAD_POOL 
)

Resizes the thread pool so that it contains at least minThreads and at most maxThreads. This function fails if maxThreads is larger than the number of worker threads (i.e. the value returned by entries()). Otherwise, idle threads are created or destroyed as necessary. Returns true if the resize was successful, otherwise false.

size_t RWThreadPool::size (  )  const

Returns the size of the thread pool, that is, the number of threads currently in the pool.

void RWThreadPool::stop (  ) 

Stops work execution after the thread pool executes all the work that is currently enqueued. No additional work is enqueued after stop() is called.


Friends And Related Function Documentation

const size_t RW_THR_NO_DYNAMIC_THREAD_POOL [related]

Constant to indicate that dynamic thread pools are not enabled.

const unsigned long RW_THR_NO_TIMEOUT [related]

Constant to indicate no timeout value.

 All Classes Functions Variables Typedefs Enumerations Enumerator Friends

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.