Introduction
Let's ensure we understand the foundational concepts before delving further into the subjects. Here is a brief introduction if you are unfamiliar with Thread pooling in ATL.
The Active Template Library (ATL) is a Microsoft library that provides a set of C++ templates to simplify the development of COM objects and other Windows-based applications.

In the Active Template Library (ATL), a thread pool is a group of worker threads used to perform tasks in parallel. The ATL manages the thread pool, which creates and manages the threads and provides an interface for scheduling tasks to be executed on the threads. Using a thread pool can help improve an application's performance and responsiveness by allowing multiple tasks to be executed in parallel rather than sequentially.
The article explains the details of thread pooling class-CComAutoThreadModule. Let's get started.
CComAutoThreadModule Class
The CComAutoThreadModule class is a part of the ATL thread pooling implementation.
Note: It is impossible to use this class or any of its members in Windows Runtime-based applications.
Syntax
template <class ThreadAllocator = CComSimpleThreadAllocator>
class CComAutoThreadModule : public CComModule
Parameters
-
ThreadAllocator
[in] The class is responsible for selecting threads. CComSimpleThreadAllocator is the default value.
Members
Methods
| Function | Description |
| CreateInstance | Generates an item in the connected apartment after choosing a thread. |
| GetDefaultThreads | (Static) based on the number of processors, dynamically determines the number of threads for the module. |
| Init | Creates the threads for the module. |
| Lock | Increases the number of locks for the module and the active thread. |
| Unlock | Reduces the number of locks on the module and the active thread. |
Data Members
| Data member | Description |
| dwThreadID | Contains the identifier of the current thread. |
| m_Allocator | Manages thread selection. |
| m_nThreads | Provides the module's thread count. |
| m_pApartments | Controls the apartments within the module. |
Remarks
To develop a thread-pooled, apartment-model COM server for EXEs and Windows services, CComAutoThreadModule inherits from CComModule. Every ThreadThread in the module has its apartment, managed by CComAutoThreadModule using CComApartment.
If you want to construct objects in many apartments, derive your module from CComAutoThreadModule. To designate CComClassFactoryAutoThread as the class factory, you must include the macro DECLARE CLASSFACTORY AUTO THREAD in the class specification of your object.
Your module will, by default, be derived from CComModule by the ATL COM AppWizard (the ATL Project Wizard in Visual Studio.NET). Modify the class definition to utilize CComAutoThreadModule. For example:
class CModule :
public CComAutoThreadModule<CComSimpleThreadAllocator>
{
public:
LONG Unlock()
{
LONG a = CComAutoThreadModule<CComSimpleThreadAllocator>::Unlock();
if (a == 0)
PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
return a;
}
DWORD dwThreadID;
};
Requirements
Header: atlbase.h



