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>
Methods | |
CreateInstance | Selects a thread and then creates an object in the associated apartment. |
GetDefaultThreads | Dynamically calculates the number of threads for the module based on the number of processors. |
Init | Creates the module's threads. |
Lock | Increments the lock count on the module and on the current thread. |
Unlock | Decrements the lock count on the module and on the current thread. |
Data Members | |
dwThreadID | Contains the identifier of the current thread. |
m_Allocator | Manages thread selection. |
m_nThreads | Contains the number of threads in the module. |
m_pApartments | Manages the module's apartments. |
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
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
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
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
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
DWORD dwThreadID;
Remarks
Contains the identifier of the current thread.
CComAutoThreadModule Overview | Class Members
ThreadAllocator m_Allocator;
Remarks
The object managing thread selection. You can use CComSimpleThreadAllocator for the ThreadAllocator class template parameter.
CComAutoThreadModule Overview | Class Members
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 threads associated apartment is managed by a CComApartment object.
CComAutoThreadModule Overview | Class Members
See Also 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.