Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

critical_section Class

 

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

A non-reentrant mutex which is explicitly aware of the Concurrency Runtime.

class critical_section;

Public Typedefs

NameDescription
native_handle_typeA reference to a critical_section object.

Public Classes

NameDescription
critical_section::scoped_lock ClassAn exception safe RAII wrapper for a critical_section object.

Public Constructors

NameDescription
critical_section::critical_section ConstructorConstructs a new critical section.
critical_section::~critical_section DestructorDestroys a critical section.

Public Methods

NameDescription
critical_section::lock MethodAcquires this critical section.
critical_section::native_handle MethodReturns a platform specific native handle, if one exists.
critical_section::try_lock MethodTries to acquire the lock without blocking.
critical_section::try_lock_for MethodTries to acquire the lock without blocking for a specific number of milliseconds.
critical_section::unlock MethodUnlocks the critical section.

For more information, see Synchronization Data Structures.

critical_section

Header: concrt.h

Namespace: concurrency

Constructs a new critical section.

critical_section();

Destroys a critical section.

~critical_section();

Remarks

It is expected that the lock is no longer held when the destructor runs. Allowing the critical section to destruct with the lock still held results in undefined behavior.

Acquires this critical section.

void lock();

Remarks

It is often safer to utilize the scoped_lock construct to acquire and release a critical_section object in an exception safe way.

If the lock is already held by the calling context, an improper_lock exception will be thrown.

Returns a platform specific native handle, if one exists.

native_handle_type native_handle();

Return Value

A reference to the critical section.

Remarks

A critical_section object is not associated with a platform specific native handle for the Windows operating system. The method simply returns a reference to the object itself.

An exception safe RAII wrapper for a critical_section object.

class scoped_lock;

Constructs a scoped_lock object and acquires the critical_section object passed in the _Critical_section parameter. If the critical section is held by another thread, this call will block.

explicit _CRTIMP scoped_lock(critical_section& _Critical_section);

Parameters

_Critical_section
The critical section to lock.

Destroys a scoped_lock object and releases the critical section supplied in its constructor.

~scoped_lock();

Tries to acquire the lock without blocking.

bool try_lock();

Return Value

If the lock was acquired, the value true; otherwise, the value false.

Tries to acquire the lock without blocking for a specific number of milliseconds.

bool try_lock_for(unsigned int _Timeout);

Parameters

_Timeout
The number of milliseconds to wait before timing out.

Return Value

If the lock was acquired, the value true; otherwise, the value false.

Unlocks the critical section.

void unlock();

concurrency Namespace
reader_writer_lock Class

Show:
© 2017 Microsoft