CThreadPool Class
Collapse the table of content
Expand the table of content

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


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


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

Public Constructors




The constructor for the thread pool.


The destructor for the thread pool.

Public Methods




Implementation of IUnknown::AddRef.


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


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


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


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


Call this method to initialize the thread pool.


Implementation of IUnknown::QueryInterface.


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


Implementation of IUnknown::Release.


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


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


Call this method to shut down the thread pool.

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.


Header: atlutil.h

© 2015 Microsoft