Udostępnij za pośrednictwem


CComAutoThreadModule Class

 

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.

Important

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

Syntax

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

Parameters

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

Members

Methods

CreateInstance Selects 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.
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

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.

Remarks

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;
};

Inheritance Hierarchy

_ATL_MODULE

CAtlModule

IAtlAutoThreadModule

CAtlModuleT

CAtlAutoThreadModuleT

CComModule

CComAutoThreadModule

Requirements

Header: atlbase.h

CComAutoThreadModule::CreateInstance

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.

CComAutoThreadModule::dwThreadID

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.

CComAutoThreadModule::GetDefaultThreads

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.

CComAutoThreadModule::Init

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.

CComAutoThreadModule::Lock

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.

CComAutoThreadModule::m_Allocator

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.

CComAutoThreadModule::m_nThreads

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.

CComAutoThreadModule::m_pApartments

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.

CComAutoThreadModule::Unlock

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.

See Also

Class Overview
Module Classes