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.
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::critical_section Constructor | Constructs a new critical section. |
critical_section::~critical_section Destructor | Destroys a critical section. |
Public Methods
Name | Description |
---|---|
critical_section::lock Method | Acquires this critical section. |
critical_section::native_handle Method | Returns a platform specific native handle, if one exists. |
critical_section::try_lock Method | Tries to acquire the lock without blocking. |
critical_section::try_lock_for Method | Tries to acquire the lock without blocking for a specific number of milliseconds. |
critical_section::unlock Method | Unlocks the critical section. |
Remarks
For more information, see Synchronization Data Structures.
Inheritance Hierarchy
critical_section
Requirements
Header: concrt.h
Namespace: concurrency
critical_section::critical_section Constructor
Constructs a new critical section.
critical_section();
critical_section::~critical_section Destructor
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.
critical_section::lock Method
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.
critical_section::native_handle Method
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;
critical_section::scoped_lock::scoped_lock Constructor
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.
critical_section::scoped_lock::~scoped_lock Destructor
Destroys a scoped_lock
object and releases the critical section supplied in its constructor.
~scoped_lock();
critical_section::try_lock Method
Tries to acquire the lock without blocking.
bool try_lock();
Return Value
If the lock was acquired, the value true
; otherwise, the value false
.
critical_section::try_lock_for Method
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
.
critical_section::unlock Method
Unlocks the critical section.
void unlock();