Partager via


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.

Syntax

class CComCriticalSection

Members

Public Constructors

Name Description
CComCriticalSection::CComCriticalSection The constructor.

Public Methods

Name Description
CComCriticalSection::Init Creates and initializes a critical section object.
CComCriticalSection::Lock Obtains ownership of the critical section object.
CComCriticalSection::Term Releases system resources used by the critical section object.
CComCriticalSection::Unlock Releases ownership of the critical section object.

Public Data Members

Name Description
CComCriticalSection::m_sec A CRITICAL_SECTION object.

Remarks

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.

Requirements

Header: atlcore.h

CComCriticalSection::CComCriticalSection

The constructor.

CComCriticalSection() throw();

Remarks

Sets the m_sec data member to NULL .

CComCriticalSection::Init

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.

CComCriticalSection::Lock

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.

CComCriticalSection::m_sec

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

CRITICAL_SECTION m_sec;

CComCriticalSection::Term

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.

CComCriticalSection::Unlock

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.

See Also

CComFakeCriticalSection Class
Class Overview
CComCritSecLock Class