RWThreadPool RWHandleBase
#include <rw/thread/RWThreadPool.h> RWThreadPool threadPool = RWThreadPool::make(10);
//Makes a thread pool of 10 threads.
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 that they would like to have executed. Pool threads dequeue functors and execute them to completion. This process continues until the thread pool object passes out of scope, the 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.
#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; } void 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(); };
RWThreadPool(void);
Constructs an empty (invalid) handle.
RWThreadPool(const RWThreadPool& second);
Constructs an external interface handle to the thread pool object that is pointed to by a second handle (if any).
RWThreadPool& operator=(const RWThreadPool& second);
Binds this external interface handle to the thread pool object that is pointed to by a second handle (if any).
void 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 will be ignored in the release version of the compiled source code. During initial development, therefore, you should compile source code in debug mode, which will throw assertions when invalid functors are found or uncaught exceptions are thrown.
size_t entries(void) const;
Returns the number of work entries queued in the thread pool.
RWThreadAttribute getPoolAttribute(void);
Gets a handle to the thread attribute instance specified during thread pool construction.
static RWThreadPool make(size_t minThreads, size_t maxThreads = RW_THR_NO_DYNAMIC_THREAD_POOL, unsigned long timeout = RW_THR_NO_TIMEOUT);
Makes a thread pool instance with a pool that contains from minThreads to maxThreads threads. The actual number of threads will grow and shrink between minThreads and maxThreads, depending on the work load. A thread will await work for at least timeout milliseconds before exiting. The thread pool threads are created with default thread attributes.
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);
Makes a thread pool instance with a pool that contains from minThreads to maxThreads threads, each created with the specified thread attributes. The actual number of threads will grow and shrink between minThreads and maxThreads, depending on the work load. A thread will await work for at least timeout milliseconds before exiting.
You are responsible for determining the suitability of thread pool attributes. For example, if you set the start policy of poolThreadsAttr to RW_THR_START_INTERRUPTED, then RWThreadPool would hang because all of the threads in the thread pool would be waiting for RWRunnable::releaseInterrupt() calls.
size_t size(void) const;
Returns the size of the thread pool, that is, the number of threads currently in the pool.
void stop(void);
Stops work execution after the thread pool executes all the work that is currently enqueued. No additional work may be enqueued after stop() is called.
RWThread, RWFunctor0, RWHandleBase::isValid()
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.