CComMultiThreadModel

class CComMultiThreadModel

CComMultiThreadModel provides thread-safe methods for incrementing and decrementing the value of a variable. Typically, you use CComMultiThreadModel through one of two typedef names, either CComObjectThreadModel or CComGlobalsThreadModel. The class referenced by each typedef depends on the threading model used, as shown in the following table:

Threading Model
typedefSingleApartmentFree
CComObjectThreadModelSSM
CComGlobalsThreadModelSMM

S=CComSingleThreadModel; M=CComMultiThreadModel

CComMultiThreadModel itself defines three typedef names. AutoCriticalSection and CriticalSection reference classes that provide methods for obtaining and releasing ownership of a critical section. ThreadModelNoCS references class CComMultiThreadModelNoCS.

#include <atlbase.h>

See Also CComSingleThreadModel, CComAutoCriticalSection, CComCriticalSection


CComMultiThreadModel Class Members

Methods
DecrementDecrements the value of the specified variable in a thread-safe manner.
IncrementIncrements the value of the specified variable in a thread-safe manner.
Typedefs
AutoCriticalSectionReferences class CComAutoCriticalSection.
CriticalSectionReferences class CComCriticalSection.
ThreadModelNoCSReferences class CComMultiThreadModelNoCS.

CComMultiThreadModel Overview


Methods


CComMultiThreadModel::Decrement

static ULONG Decrement( LPLONG p );

Return Value

If the result of the decrement is 0, then Decrement returns 0. If the result of the decrement is nonzero, the return value is also nonzero but may not equal the result of the decrement.

Parameters

p [in] Pointer to the variable to be decremented.

Remarks

This method calls the Win32 API function, InterlockedDecrement, which decrements the value of the variable pointed to by p. InterlockedDecrement prevents more than one thread from simultaneously using this variable.

CComMultiThreadModel Overview | Class Members

See Also CComMultiThreadModel::Increment, InterlockedDecrement in the Win32 SDK


CComMultiThreadModel::Increment

static ULONG Increment( LPLONG p );

Return Value

If the result of the increment is 0, then Increment returns 0. If the result of the increment is nonzero, the return value is also nonzero but may not equal the result of the increment.

Parameters

p [in] Pointer to the variable to be incremented.

Remarks

This method calls the Win32 API function, InterlockedIncrement, which increments the value of the variable pointed to by p. InterlockedIncrement prevents more than one thread from simultaneously using this variable.

CComMultiThreadModel Overview | Class Members

See Also CComMultiThreadModel::Decrement, InterlockedIncrement in the Win32 SDK


Typedefs


CComMultiThreadModel::AutoCriticalSection

typedef CComAutoCriticalSection AutoCriticalSection;

Remarks

When using CComMultiThreadModel, the typedef name AutoCriticalSection references class CComAutoCriticalSection, which provides methods for obtaining and releasing ownership of a critical section object.

CComSingleThreadModel and CComMultiThreadModelNoCS also contain definitions for AutoCriticalSection. The following table shows the relationship between the threading model class and the class referenced by AutoCriticalSection:

Class defined inClass referenced
CComMultiThreadModelCComAutoCriticalSection
CComSingleThreadModelCComFakeCriticalSection
CComMultiThreadModelNoCSCComFakeCriticalSection

In addition to AutoCriticalSection, you can use the typedef name CriticalSection. You should not specify AutoCriticalSection in global objects or static class members if you want to eliminate the CRT startup code.

Example

The following code is taken from CComObjectRootEx.

template< class ThreadModel >
class CComObjectRootEx : public CComObjectRootBase
{
public:
   typedef ThreadModel _ThreadModel;
   typedef _ThreadModel::AutoCriticalSection _CritSec;
   ULONG InternalAddRef( )
   {
      ...
      return _ThreadModel::Increment(&m_dwRef);
   }
   ...
   void Lock( ) { m_critsec.Lock( ); }
   ...
private:
   _CritSec m_critsec;
};

The following tables show the results of the InternalAddRef and Lock methods, depending on the ThreadModel template parameter and the threading model used by the application:

ThreadModel = CComObjectThreadModel

Single or ApartmentFree
InternalAddRefThe increment is not thread-safe. The increment is thread-safe.
LockDoes nothing; there is no critical section to lock.The critical section is locked.

ThreadModel = CComObjectThreadModel::ThreadModelNoCS

Single or ApartmentFree
InternalAddRefThe increment is not thread-safe.The increment is thread-safe.
LockDoes nothing; there is no critical section to lock.Does nothing; there is no critical section to lock.

CComMultiThreadModel Overview | Class Members

See Also CComObjectThreadModel, CComGlobalsThreadModel, CComMultiThreadModel::ThreadModelNoCS


CComMultiThreadModel::CriticalSection

typedef CComCriticalSection CriticalSection;

Remarks

When using CComMultiThreadModel, the typedef name CriticalSection references class CComCriticalSection, which provides methods for obtaining and releasing ownership of a critical section object.

CComSingleThreadModel and CComMultiThreadModelNoCS also contain definitions for CriticalSection. The following table shows the relationship between the threading model class and the class referenced by CriticalSection:

Class defined inClass referenced
CComMultiThreadModelCComCriticalSection
CComSingleThreadModelCComFakeCriticalSection
CComMultiThreadModelNoCSCComFakeCriticalSection

In addition to CriticalSection, you can use the typedef name AutoCriticalSection. You should not specify AutoCriticalSection in global objects or static class members if you want to eliminate the CRT startup code.

Example

See AutoCriticalSection.

CComMultiThreadModel Overview | Class Members

See Also CComObjectThreadModel, CComGlobalsThreadModel, CComMultiThreadModel::ThreadModelNoCS


CComMultiThreadModel::ThreadModelNoCS

typedef CComMultiThreadModelNoCS ThreadModelNoCS;

Remarks

When using CComMultiThreadModel, the typedef name ThreadModelNoCS references class CComMultiThreadModelNoCS. CComMultiThreadModelNoCS provides thread-safe methods for imcrementing and decrementing a variable; however, it does not provide a critical section.

CComSingleThreadModel and CComMultiThreadModelNoCS also contain definitions for ThreadModelNoCS. The following table shows the relationship between the threading model class and the class referenced by ThreadModelNoCS:

Class defined inClass referenced
CComMultiThreadModelCComMultiThreadModelNoCS
CComSingleThreadModelCComSingleThreadModel
CComMultiThreadModelNoCSCComMultiThreadModelNoCS

Example

See AutoCriticalSection.

CComMultiThreadModel Overview | Class Members

See Also CComObjectThreadModel, CComGlobalsThreadModel