Share via


mutex Class (STL)

 

The latest version of this topic can be found at mutex Class (C++ Standard Library)| Microsoft Docs.

Represents a mutex type. Objects of this type can be used to enforce mutual exclusion within a program.

Syntax

class mutex;

Members

Public Constructors

Name Description
mutex::mutex Constructor Constructs a mutex object.
mutex::~mutex Destructor Releases any resources that were used by the mutex object.

Public Methods

Name Description
mutex::lock Method Blocks the calling thread until the thread obtains ownership of the mutex.
mutex::native_handle Method Returns the implementation-specific type that represents the mutex handle.
mutex::try_lock Method Attempts to obtain ownership of the mutex without blocking.
mutex::unlock Method Releases ownership of the mutex.

Requirements

Header: mutex

Namespace: std

mutex::lock Method

Blocks the calling thread until the thread obtains ownership of the mutex.

void lock();

Remarks

If the calling thread already owns the mutex, the behavior is undefined.

mutex::mutex Constructor

Constructs a mutex object that is not locked.

constexpr mutex() noexcept;

mutex::~mutex Destructor

Releases any resources that are used by the mutex object.

~mutex();

Remarks

If the object is locked when the destructor runs, the behavior is undefined.

mutex::native_handle Method

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();

Return Value

native_handle_type is defined as a Concurrency::critical_section * that's cast as void *.

mutex::try_lock Method

Attempts to obtain ownership of the mutex without blocking.

bool try_lock();

Return Value

true if the method successfully obtains ownership of the mutex; otherwise, false.

Remarks

If the calling thread already owns the mutex, the behavior is undefined.

mutex::unlock Method

Releases ownership of the mutex.

void unlock();

Remarks

If the calling thread does not own the mutex, the behavior is undefined.

See Also

Header Files Reference
<mutex>