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 critical section class reference by AutoCriticalSection:

Class defined in Class referenced
CComMultiThreadModel CComCriticalSection
CComSingleThreadModel CComFakeCriticalSection
CComMultiThreadModelNoCS CComFakeCriticalSection

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 Apartment Free
InternalAddRef The increment is not thread-safe. The increment is thread-safe.
Lock Does nothing; there is no critical section to lock. The critical section is locked.

ThreadModel = CComObjectThreadModel::ThreadModelNoCS

Single or Apartment Free
InternalAddRef The increment is not thread-safe. The increment is thread-safe.
Lock Does nothing; there is no critical section to lock. Does nothing; there is no critical section to lock.

CComMultiThreadModel OverviewClass Members

See Also   CComObjectThreadModel, CComGlobalsThreadModel, CComMultiThreadModel::ThreadModelNoCS