Share via


CComMultiThreadModel::AutoCriticalSection

 

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

Syntax

typedef CComAutoCriticalSection AutoCriticalSection;

Remarks

CComSingleThreadModel and CComMultiThreadModelNoCS also contain definitions for AutoCriticalSection. The following table shows the relationship between the threading model class and the critical section class referenced 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 modeled after CComObjectRootEx, and demonstrates AutoCriticalSection being used in a threading environment.

template< class ThreadModel >
class CMyAutoCritClass
{
public:
   typedef ThreadModel _ThreadModel;
   typedef typename _ThreadModel::AutoCriticalSection _CritSec;

   CMyAutoCritClass() : m_dwRef(0) {}

   ULONG InternalAddRef()
   {
      return _ThreadModel::Increment(&m_dwRef);
   }
   ULONG InternalRelease()
   {
      return _ThreadModel::Decrement(&m_dwRef);   
   }
   void Lock() { m_critsec.Lock( ); }
   void Unlock() { m_critsec.Unlock(); }

private:
   _CritSec m_critsec;
   LONG m_dwRef;
};

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

Method

Single or Apartment Threading

Free Threading

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

Method

Single or Apartment Threading

Free Threading

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.

Requirements

Header: atlbase.h

See Also

CComMultiThreadModel Class
CComObjectThreadModel
CComGlobalsThreadModel
CComMultiThreadModel::ThreadModelNoCS