Edit

Share via


CriticalSection Class

Represents a critical section object.

Syntax

class CriticalSection;

Members

Constructor

Name Description
CriticalSection::CriticalSection Initializes a synchronization object that is similar to a mutex object, but can be used by only the threads of a single process.
CriticalSection::~CriticalSection Deinitializes and destroys the current CriticalSection object.

Public Methods

Name Description
CriticalSection::IsValid Indicates whether the current critical section is valid.
CriticalSection::Lock Waits for ownership of the specified critical section object. The function returns when the calling thread is granted ownership.
CriticalSection::TryLock Attempts to enter a critical section without blocking. If the call is successful, the calling thread takes ownership of the critical section.

Protected Data Members

Name Description
CriticalSection::cs_ Declares a critical section data member.

Inheritance Hierarchy

CriticalSection

Requirements

Header: corewrappers.h

Namespace: Microsoft::WRL::Wrappers

CriticalSection::~CriticalSection

Deinitializes and destroys the current CriticalSection object.

WRL_NOTHROW ~CriticalSection();

CriticalSection::CriticalSection

Initializes a synchronization object that is similar to a mutex object, but can be used by only the threads of a single process.

explicit CriticalSection(
   ULONG spincount = 0
);

Parameters

spincount
The spin count for the critical section object. The default value is 0.

Remarks

For more information about critical sections and spincounts, see the InitializeCriticalSectionAndSpinCount function in the Synchronization section of the Windows API documentation.

CriticalSection::cs_

Declares a critical section data member.

CRITICAL_SECTION cs_;

Remarks

This data member is protected.

CriticalSection::IsValid

Indicates whether the current critical section is valid.

bool IsValid() const;

Return Value

By default, always returns true.

CriticalSection::Lock

Waits for ownership of the specified critical section object. The function returns when the calling thread is granted ownership.

SyncLock Lock();

   static SyncLock Lock(
   _In_ CRITICAL_SECTION* cs
);

Parameters

cs
A user-specified critical section object.

Return Value

A lock object that can be used to unlock the current critical section.

Remarks

The first Lock function affects the current critical section object. The second Lock function affects a user-specified critical section.

CriticalSection::TryLock

Attempts to enter a critical section without blocking. If the call is successful, the calling thread takes ownership of the critical section.

SyncLock TryLock();

static SyncLock TryLock(
   _In_ CRITICAL_SECTION* cs
);

Parameters

cs
A user-specified critical section object.

Return Value

A nonzero value if the critical section is successfully entered or the current thread already owns the critical section. Zero if another thread already owns the critical section.

Remarks

The first TryLock function affects the current critical section object. The second TryLock function affects a user-specified critical section.