This documentation is archived and is not being maintained.

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.


typedef CComAutoCriticalSection AutoCriticalSection;

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.

The following code is taken from CComObjectRootEx, and demonstrates AutoCriticalSection being used in a threading environment.

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

Show: