CComAutoThreadModule Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CComAutoThreadModule Class.

As of ATL 7.0, CComAutoThreadModule is obsolete: see ATL Module Classes for more details.

System_CAPS_ICON_important.jpg Important

This class and its members cannot be used in applications that execute in the Windows Runtime.

template <class ThreadAllocator = CComSimpleThreadAllocator>  
class CComAutoThreadModule : public CComModule

Parameters

ThreadAllocator
[in] The class managing thread selection. The default value is CComSimpleThreadAllocator.

Methods

CreateInstanceSelects a thread and then creates an object in the associated apartment.
GetDefaultThreads(Static) Dynamically calculates the number of threads for the module based on the number of processors.
InitCreates the module's threads.
LockIncrements the lock count on the module and on the current thread.
UnlockDecrements the lock count on the module and on the current thread.

Data Members

Data Members

dwThreadIDContains the identifier of the current thread.
m_AllocatorManages thread selection.
m_nThreadsContains the number of threads in the module.
m_pApartmentsManages the module's apartments.
System_CAPS_ICON_note.jpg Note

This class is obsolete, having been replaced by the CAtlAutoThreadModule and CAtlModule derived classes. The information that follows is for use with older releases of ATL.

CComAutoThreadModule derives from CComModule to implement a thread-pooled, apartment-model COM server for EXEs and Windows services. CComAutoThreadModule uses CComApartment to manage an apartment for each thread in the module.

Derive your module from CComAutoThreadModule when you want to create objects in multiple apartments. You must also include the DECLARE_CLASSFACTORY_AUTO_THREAD macro in your object's class definition to specify CComClassFactoryAutoThread as the class factory.

By default, the ATL COM AppWizard (the ATL Project Wizard in Visual Studio .NET) will derive your module from CComModule. To use CComAutoThreadModule, modify the class definition. For example:

class CMyModule : 
public CComAutoThreadModule<CComSimpleThreadAllocator>
{
public:
   LONG Unlock()
   {
      LONG l = CComAutoThreadModule<CComSimpleThreadAllocator>::Unlock();
      if (l == 0)
         PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
      return l;
   }

   DWORD dwThreadID;
};

_ATL_MODULE

CAtlModule

IAtlAutoThreadModule

CAtlModuleT

CAtlAutoThreadModuleT

CComModule

CComAutoThreadModule

Header: atlbase.h

As of ATL 7.0, CComAutoThreadModule is obsolete: see ATL Module Classes for more details.

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

Parameters

pfnCreateInstance
[in] A pointer to a creator function.

riid
[in] The IID of the requested interface.

ppvObj
[out] A pointer to the interface pointer identified by riid. If the object does not support this interface, ppvObj is set to NULL.

Return Value

A standard HRESULT value.

Remarks

Selects a thread and then creates an object in the associated apartment.

As of ATL 7.0, CComAutoThreadModule is obsolete: see ATL Module Classes for more details.

DWORD dwThreadID;

Remarks

Contains the identifier of the current thread.

As of ATL 7.0, CComAutoThreadModule is obsolete: see ATL Module Classes for more details.

static int GetDefaultThreads();

Return Value

The number of threads to be created in the EXE module.

Remarks

This static function dynamically calculates the maximum number of threads for the EXE module, based on the number of processors. By default, this return value is passed to the Init method to create the threads.

As of ATL 7.0, CComAutoThreadModule is obsolete: see ATL Module Classes for more details.

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

Parameters

p
[in] A pointer to an array of object map entries.

h
[in] The HINSTANCE passed to DLLMain or WinMain.

plibid
[in] A pointer to the LIBID of the type library associated with the project.

nThreads
[in] The number of threads to be created. By default, nThreads is the value returned by GetDefaultThreads.

Remarks

Initializes data members and creates the number of threads specified by nThreads.

As of ATL 7.0, CComAutoThreadModule is obsolete: see ATL Module Classes for more details.

LONG Lock();

Return Value

A value that may be useful for diagnostics or testing.

Remarks

Performs an atomic increment on the lock count for the module and for the current thread. CComAutoThreadModule uses the module lock count to determine whether any clients are accessing the module. The lock count on the current thread is used for statistical purposes.

As of ATL 7.0, CComAutoThreadModule is obsolete: see ATL Module Classes for more details.

ThreadAllocator  m_Allocator;

Remarks

The object managing thread selection. By default, the ThreadAllocator class template parameter is CComSimpleThreadAllocator.

As of ATL 7.0, CComAutoThreadModule is obsolete: see ATL Module Classes for more details.

int m_nThreads;

Remarks

Contains the number of threads in the EXE module. When Init is called, m_nThreads is set to the nThreads parameter value. Each thread's associated apartment is managed by a CComApartment object.

As of ATL 7.0, CComAutoThreadModule is obsolete: see ATL Module Classes for more details.

CComApartment* m_pApartments;

Remarks

Points to an array of CComApartment objects, each of which manages an apartment in the module. The number of elements in the array is based on the m_nThreads member.

As of ATL 7.0, CComAutoThreadModule is obsolete: see ATL Module Classes for more details.

LONG Unlock();

Return Value

A value that may be useful for diagnostics or testing.

Remarks

Performs an atomic decrement on the lock count for the module and for the current thread. CComAutoThreadModule uses the module lock count to determine whether any clients are accessing the module. The lock count on the current thread is used for statistical purposes.

When the module lock count reaches zero, the module can be unloaded.

Class Overview
Module Classes

Show: