CThreadPool Class

This class provides a pool of worker threads that process a queue of work items.

template < 
   class Worker, 
   class ThreadTraits = DefaultThreadTraits 
> 
class CThreadPool : 
   public IThreadPoolConfig

Parameters

  • Worker
    The class conforming to the worker archetype providing the code used to process work items queued on the thread pool.

  • ThreadTraits
    The class providing the function used to create the threads in the pool.

Members

Public Constructors

Name

Description

CThreadPool::CThreadPool

The constructor for the thread pool.

CThreadPool::~CThreadPool

The destructor for the thread pool.

Public Methods

Name

Description

CThreadPool::AddRef

Implementation of IUnknown::AddRef.

CThreadPool::GetNumThreads

Call this method to get the number of threads in the pool.

CThreadPool::GetQueueHandle

Call this method to get the handle of the IO completion port used to queue work items.

CThreadPool::GetSize

Call this method to get the number of threads in the pool.

CThreadPool::GetTimeout

Call this method to get the maximum time in milliseconds that the thread pool will wait for a thread to shut down.

CThreadPool::Initialize

Call this method to initialize the thread pool.

CThreadPool::QueryInterface

Implementation of IUnknown::QueryInterface.

CThreadPool::QueueRequest

Call this method to queue a work item to be handled by a thread in the pool.

CThreadPool::Release

Implementation of IUnknown::Release.

CThreadPool::SetSize

Call this method to set the number of threads in the pool.

CThreadPool::SetTimeout

Call this method to set the maximum time in milliseconds that the thread pool will wait for a thread to shut down.

CThreadPool::Shutdown

Call this method to shut down the thread pool.

Remarks

Threads in the pool are created and destroyed when the pool is initialized, resized, or shut down. An instance of class Worker will be created on the stack of each worker thread in the pool. Each instance will live for the lifetime of the thread.

Immediately after creation of a thread, Worker::Initialize will be called on the object associated with that thread. Immediately before destruction of a thread, Worker::Terminate will be called. Both methods must accept a void* argument. The value of this argument is passed to the thread pool through the pvWorkerParam parameter of CThreadPool::Initialize.

When there are work items in the queue and worker threads available for work, a worker thread will pull an item off the queue and call the Execute method of the Worker object for that thread. Three items are then passed to the method: the item from the queue, the same pvWorkerParam passed to Worker::Initialize and Worker::Terminate, and a pointer to the OVERLAPPED structure used for the IO completion port queue.

The Worker class declares the type of the items that will be queued on the thread pool by providing a typedef, Worker::RequestType. This type must be capable of being cast to and from a ULONG_PTR.

An example of a Worker class is CNonStatelessWorker Class.

Inheritance Hierarchy

IUnknown

IThreadPoolConfig

CThreadPool

Requirements

Header: atlutil.h

See Also

Reference

IThreadPoolConfig Interface

DefaultThreadTraits

Other Resources

ATL Classes