Muokkaa

Jaa


critical_section Class

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

Syntax

class critical_section;

Members

Public Typedefs

Name Description
native_handle_type A reference to a critical_section object.

Public Classes

Name Description
critical_section::scoped_lock Class An exception safe RAII wrapper for a critical_section object.

Public Constructors

Name Description
critical_section Constructs a new critical section.
~critical_section Destructor Destroys a critical section.

Public Methods

Name Description
lock Acquires this critical section.
native_handle Returns a platform specific native handle, if one exists.
try_lock Tries to acquire the lock without blocking.
try_lock_for Tries to acquire the lock without blocking for a specific number of milliseconds.
unlock Unlocks the critical section.

Remarks

For more information, see Synchronization Data Structures.

Inheritance Hierarchy

critical_section

Requirements

Header: concrt.h

Namespace: concurrency

critical_section

Constructs a new critical section.

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.

lock

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.

native_handle

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.

critical_section::scoped_lock Class

An exception safe RAII wrapper for a critical_section object.

class scoped_lock;

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

scoped_lock::~scoped_lock

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

~scoped_lock();

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

try_lock_for

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.

unlock

Unlocks the critical section.

void unlock();

See also

concurrency Namespace
reader_writer_lock Class