CComCriticalSection Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CComCriticalSection Class.

This class provides methods for obtaining and releasing ownership of a critical section object.

class CComCriticalSection

Public Constructors

NameDescription
CComCriticalSection::CComCriticalSectionThe constructor.

Public Methods

NameDescription
CComCriticalSection::InitCreates and initializes a critical section object.
CComCriticalSection::LockObtains ownership of the critical section object.
CComCriticalSection::TermReleases system resources used by the critical section object.
CComCriticalSection::UnlockReleases ownership of the critical section object.

Public Data Members

NameDescription
CComCriticalSection::m_secA CRITICAL_SECTION object.

CComCriticalSection is similar to class CComAutoCriticalSection, except that you must explicitly initialize and release the critical section.

Typically, you use CComCriticalSection through the typedef name CriticalSection. This name references CComCriticalSection when CComMultiThreadModel is being used.

See CComCritSecLock Class for a safer way to use this class than calling Lock and Unlock directly.

Header: atlcore.h

The constructor.

CComCriticalSection() throw();

Remarks

Sets the m_sec data member to NULL .

Calls the Win32 function InitializeCriticalSection, which initializes the critical section object contained in the m_sec data member.

HRESULT Init() throw();

Return Value

Returns S_OK on success, E_OUTOFMEMORY or E_FAIL on failure.

Calls the Win32 function EnterCriticalSection, which waits until the thread can take ownership of the critical section object contained in the m_sec data member.

HRESULT Lock() throw();

Return Value

Returns S_OK on success, E_OUTOFMEMORY or E_FAIL on failure.

Remarks

The critical section object must first be initialized with a call to the Init method. When the protected code has finished executing, the thread must call Unlock to release ownership of the critical section.

Contains a critical section object that is used by all CComCriticalSection methods.

CRITICAL_SECTION m_sec;

Calls the Win32 function DeleteCriticalSection, which releases all resources used by the critical section object contained in the m_sec data member.

HRESULT Term() throw();

Return Value

Returns S_OK.

Remarks

Once Term has been called, the critical section can no longer be used for synchronization.

Calls the Win32 function LeaveCriticalSection, which releases ownership of the critical section object contained in the m_sec data member.

HRESULT Unlock() throw();

Return Value

Returns S_OK.

Remarks

To first obtain ownership, the thread must call the Lock method. Each call to Lock requires a corresponding call to Unlock to release ownership of the critical section.

CComFakeCriticalSection Class
Class Overview
CComCritSecLock Class

Show: