CComAutoThreadModule

template< class ThreadAllocator >
class CComAutoThreadModule : public CComModule

Parameters

ThreadAllocator [in] The class managing thread selection. You can use CComSimpleThreadAllocator for this parameter.

CComAutoThreadModule derives from CComModule to implement a thread-pooled, apartment-model COM server for EXEs and Windows NT 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 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<ComSimpleThreadAllocator>::Unlock( ); 
      if (l == 0) 
         PostThreadMessage(dwThreadID, WM_QUIT, 0, 0); 
      return l; 
   } 
   DWORD dwThreadID; 
}; 

For more information about the AppWizard, see the article Creating an ATL Project.

#include <atlbase.h>


CComAutoThreadModule Class Members

Methods
CreateInstanceSelects a thread and then creates an object in the associated apartment.
GetDefaultThreadsDynamically 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
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.

CComAutoThreadModule Overview


Methods


CComAutoThreadModule::CreateInstance

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

Return Value

A standard HRESULT value.

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.

Remarks

Selects a thread and then creates an object in the associated apartment. Called by CComClassFactoryAutoThread::CreateInstance.

CComAutoThreadModule Overview | Class Members


CComAutoThreadModule::GetDefaultThreads

static int GetDefaultThreads( );

Return Value

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

Remarks

This static method 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.

CComAutoThreadModule Overview | Class Members


CComAutoThreadModule::Init

void Init( _ATL_OBJMAP_ENTRY* p, HINSTANCE h, int nThreads = GetDefaultThreads( ) );

Parameters

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

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

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.

CComAutoThreadModule Overview | Class Members

See Also CComAutoThreadModule::m_nThreads


CComAutoThreadModule::Lock

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.

CComAutoThreadModule Overview | Class Members

See Also CComAutoThreadModule::Unlock, CComModule::m_nLockCnt, CComApartment::m_nLockCnt


CComAutoThreadModule::Unlock

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.

CComAutoThreadModule Overview | Class Members

See Also CComAutoThreadModule::Lock, CComModule::m_nLockCnt, CComApartment::m_nLockCnt


Data Members


CComAutoThreadModule::dwThreadID

DWORD dwThreadID;

Remarks

Contains the identifier of the current thread.

CComAutoThreadModule Overview | Class Members


CComAutoThreadModule::m_Allocator

ThreadAllocator m_Allocator;

Remarks

The object managing thread selection. You can use CComSimpleThreadAllocator for the ThreadAllocator class template parameter.

CComAutoThreadModule Overview | Class Members


CComAutoThreadModule::m_nThreads

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.

CComAutoThreadModule Overview | Class Members

See Also CComAutoThreadModule::m_pApartments


CComAutoThreadModule::m_pApartments

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.

CComAutoThreadModule Overview | Class Members