Table of contents
1.
Introduction
2.
CComAutoThreadModule Class
2.1.
Syntax
2.2.
Parameters
2.3.
Members
2.3.1.
Methods
2.3.2.
Data Members
2.4.
Remarks
2.5.
Requirements
3.
CComAutoThreadModule::CreateInstance
3.1.
Parameters
3.2.
Return Value
3.3.
Remarks
4.
CComAutoThreadModule::dwThreadID
4.1.
Remarks
5.
CComAutoThreadModule::GetDefaultThreads
5.1.
Return Value
5.2.
Remarks
6.
CComAutoThreadModule::Init
6.1.
Parameters
6.2.
Remarks
7.
CComAutoThreadModule::Lock
7.1.
Return Value
7.2.
Remarks
8.
CComAutoThreadModule::m_Allocator
8.1.
Remarks
9.
CComAutoThreadModule::m_nThreads
9.1.
Remarks
10.
CComAutoThreadModule::m_pApartments
10.1.
Remarks
11.
CComAutoThreadModule::Unlock
11.1.
Return Value
11.2.
Remarks
12.
Frequently Asked Questions
12.1.
How do you manage threads in ATL?
12.2.
How can you monitor and debug ThreadThread pooling in ATL?
12.3.
How does ThreadThread pooling in ATL compare to other ThreadThread pooling libraries or techniques?
13.
Conclusion
Last Updated: Aug 13, 2025
Medium

Thread pooling class-CComAutoThreadModule

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Introduction

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

CComAutoThreadModule::CreateInstance

HRESULT CreateInstance(
    void* pfnCreateInstance,
    REFIID riid,
    void** ppvObj);

Parameters

  • pfnCreateInstance
    [in] A pointer to a creator function.
     
  • riid
    [in] The desired interface's IID.
     
  • ppvObj
    [out] A reference to the interface pointer that riid has identified. ppvObj is set to NULL if the object does not support this interface.

Return Value

A standard HRESULT value

Remarks

Creates an item in the connected apartment after selecting a thread.

CComAutoThreadModule::dwThreadID

DWORD dwThreadID;

Remarks

Contains the identifier of the current thread

CComAutoThreadModule::GetDefaultThreads

static int GetDefaultThreads();

Return Value

The number of threads the EXE module will produce.

Remarks

Based on the number of processors, this static function dynamically determines the maximum number of threads for the EXE module. By default, the Init function receives this return value in order to start the threads.

CComAutoThreadModule::Init

HRESULT Init(
    _ATL_OBJMAP_ENTRY* a,
    HINSTANCE b,
    const GUID* plibid = NULL,
    int nThreads = GetDefaultThreads());

Parameters

  • A
    [in] A pointer to an array of elements in an object map.
     
  • b
    [in] The HINSTANCE that DLLMain or WinMain received.
     
  • plibid
    [in] A reference to the type library's LIBID that is linked to the project.
     
  • nThreads
    [in] The number of threads to be created. By default, nThreads is the value returned by GetDefaultThreads.

Remarks

Creates the number of threads indicated by nThreads and initialises the data members.

CComAutoThreadModule::Lock

LONG Lock();

Return Value

A number that could be helpful for testing or diagnostic purposes.

Remarks

Performs an atomic increment on the module's and the active thread's lock count. The module lock count is used by CComAutoThreadModule to identify whether any clients are currently using the module. For statistics purposes, the current thread's lock count is used.

CComAutoThreadModule::m_Allocator

ThreadAllocator  m_Allocator;

Remarks

The EXE module's thread count is contained in this field. m nThreads is set to the nThreads argument value when Init is invoked. An object called CComApartment controls the apartment that each thread is connected.

CComAutoThreadModule::m_nThreads

int m_nThreads;

Remarks

The entity in charge of thread selection. CComSimpleThreadAllocator is the default value for the ThreadAllocator class template parameter.

CComAutoThreadModule::m_pApartments

CComApartment* m_pApartments;

Remarks

An atomic decrement on the module's and the active thread's lock count. The module lock count is used by CComAutoThreadModule to identify whether any clients are currently using the module. For statistics purposes, the current thread's lock count is used.

The module may be uninstalled when the number of module locks drops to zero.

CComAutoThreadModule::Unlock

LONG Unlock();

Return Value

A number that could be helpful for testing or diagnostic purposes.

Remarks

Points to a collection of CComApartment objects, each of which controls a module apartment. Based on the m nThreads attribute, the array's size is determined.

Frequently Asked Questions

How do you manage threads in ATL?

ATL provides a set of classes and functions for creating and managing COM objects but doesn't provide specific functionality for managing threads. However, you can use other libraries or classes in C++ to manage threads in an ATL-based project.

How can you monitor and debug ThreadThread pooling in ATL?

To monitor and debug ThreadThread pooling in ATL, you can use tools such as a debugger or a profiler to track the execution of threads and identify any issues or bottlenecks. Additionally, you can use logging or other diagnostic techniques to track the behavior of the thread pool and the tasks it executes.

How does ThreadThread pooling in ATL compare to other ThreadThread pooling libraries or techniques?

As ATL doesn't provide a built-in thread pooling feature, it's impossible to compare it with other ThreadThread pooling libraries or techniques. But you can use other libraries or classes in C++ to implement thread pooling and compare it with others.

Conclusion

In this article, we have discussed the details of Thread pooling class-CComAutoThreadModule with their syntax and members.

We hope the blog has helped you enhance your knowledge of thread pooling class-CComAutoThreadModule

 

You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews

Do upvote our python power function blog to help other ninjas grow. 

Happy Coding

Live masterclass