description | title | ms.date | f1_keywords | helpviewer_keywords | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: mutex Class (C++ Standard Library) |
mutex Class (C++ Standard Library) |
10/22/2021 |
|
|
Represents a mutex type. Objects of this type can be used to enforce mutual exclusion within a program.
class mutex;
Name | Description |
---|---|
mutex |
Constructs a mutex object. |
~mutex |
Releases any resources that were used by the mutex object. |
Name | Description |
---|---|
lock |
Blocks the calling thread until the thread obtains ownership of the mutex . |
native_handle |
Returns the implementation-specific type that represents the mutex handle. |
try_lock |
Attempts to obtain ownership of the mutex without blocking. |
unlock |
Releases ownership of the mutex . |
Header: <mutex>
Namespace: std
Blocks the calling thread until the thread obtains ownership of the mutex
.
void lock();
If the calling thread already owns the mutex
, the behavior is undefined.
Constructs a mutex
object that isn't locked.
Before Visual Studio 2022 17.10, Microsoft's implementation of this constructor wasn't constexpr
. Now it's constexpr
.
mutex() noexcept;
Releases any resources that are used by the mutex
object.
~mutex();
If the object is locked when the destructor runs, the behavior is undefined.
Returns the implementation-specific type that represents the mutex handle. The mutex handle can be used in implementation-specific ways.
native_handle_type native_handle();
native_handle_type
is defined as a Concurrency::critical_section *
. It's cast as void *
.
Attempts to obtain ownership of the mutex
without blocking.
bool try_lock();
true
if the method successfully obtains ownership of the mutex
; otherwise, false
.
If the calling thread already owns the mutex
, the behavior is undefined.
Releases ownership of the mutex
.
void unlock();
If the calling thread doesn't own the mutex
, the behavior is undefined.