Skip to main content

C++ named requirements: Mutex (since C++11)

The Mutex requirements extends the Lockable requirements to include inter-thread synchronization.

Requirements

For an object m of Mutex type:

  • The expression m.lock() has the following properties
  • The expression m.try_lock() has the following properties
    • Behaves as an atomic operation.
    • Attempts to obtain exclusive ownership of the mutex for the calling thread without blocking. If ownership is not obtained, returns immediately. The function is allowed to spuriously fail and return even if the mutex is not currently owned by another thread.
    • If try_lock() succeeds, prior unlock() operations on the same object synchronize-with this operation (equivalent to release-acquire std::memory_order). lock() does not synchronize with a failed try_lock()
    • Does not throw exceptions.
  • The expression m.unlock() has the following properties
    • Behaves as an atomic operation.
    • Releases the calling thread's ownership of the mutex and synchronizes-with the subsequent successful lock operations on the same object.
    • The behavior is undefined if the calling thread does not own the mutex.
    • Does not throw exceptions.
  • All lock and unlock operations on a single mutex occur in a single total order that can be viewed as modification order of an atomic variable: the order is specific to this individual mutex.

Library types

The following standard library types satisfy Mutex:

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
LWG 2309C++11lock might throw std::system_error with error code std::errc::device_or_resource_busynot allowed

C++ named requirements: Mutex (since C++11)

The Mutex requirements extends the Lockable requirements to include inter-thread synchronization.

Requirements

For an object m of Mutex type:

  • The expression m.lock() has the following properties
  • The expression m.try_lock() has the following properties
    • Behaves as an atomic operation.
    • Attempts to obtain exclusive ownership of the mutex for the calling thread without blocking. If ownership is not obtained, returns immediately. The function is allowed to spuriously fail and return even if the mutex is not currently owned by another thread.
    • If try_lock() succeeds, prior unlock() operations on the same object synchronize-with this operation (equivalent to release-acquire std::memory_order). lock() does not synchronize with a failed try_lock()
    • Does not throw exceptions.
  • The expression m.unlock() has the following properties
    • Behaves as an atomic operation.
    • Releases the calling thread's ownership of the mutex and synchronizes-with the subsequent successful lock operations on the same object.
    • The behavior is undefined if the calling thread does not own the mutex.
    • Does not throw exceptions.
  • All lock and unlock operations on a single mutex occur in a single total order that can be viewed as modification order of an atomic variable: the order is specific to this individual mutex.

Library types

The following standard library types satisfy Mutex:

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
LWG 2309C++11lock might throw std::system_error with error code std::errc::device_or_resource_busynot allowed