Files
2025-10-25 03:02:53 +03:00

10 KiB

[thread.sharedmutex.requirements]

32 Concurrency support library [thread]

32.6 Mutual exclusion [thread.mutex]

32.6.4 Mutex requirements [thread.mutex.requirements]

32.6.4.4 Shared mutex types [thread.sharedmutex.requirements]

32.6.4.4.1 General [thread.sharedmutex.requirements.general]

1

#

The standard library types shared_mutex and shared_timed_mutex are shared mutex types.

Shared mutex types meet the requirements of mutex types ([thread.mutex.requirements.mutex]) and additionally meet the requirements set out below.

In this description,m denotes an object of a shared mutex type.

[Note 1:

The shared mutex types meet the Cpp17SharedLockable requirements ([thread.req.lockable.shared]).

— end note]

2

#

In addition to the exclusive lock ownership mode specified in [thread.mutex.requirements.mutex], shared mutex types provide ashared lock ownership mode.

Multiple execution agents can simultaneously hold a shared lock ownership of a shared mutex type.

But no execution agent holds a shared lock while another execution agent holds an exclusive lock on the same shared mutex type, and vice-versa.

The maximum number of execution agents which can share a shared lock on a single shared mutex type is unspecified, but is at least 10000.

If more than the maximum number of execution agents attempt to obtain a shared lock, the excess execution agents block until the number of shared locks are reduced below the maximum amount by other execution agents releasing their shared lock.

3

#

The expression m.lock_shared() is well-formed and has the following semantics:

4

#

Preconditions: The calling thread has no ownership of the mutex.

5

#

Effects: Blocks the calling thread until shared ownership of the mutex can be obtained for the calling thread.

If an exception is thrown then a shared lock has not been acquired for the current thread.

6

#

Synchronization: Prior unlock() operations on the same object synchronize with ([intro.multithread]) this operation.

7

#

Postconditions: The calling thread has a shared lock on the mutex.

8

#

Return type: void.

9

#

Throws: system_error when an exception is required ([thread.req.exception]).

10

#

Error conditions:

  • (10.1)

    operation_not_permitted — if the thread does not have the privilege to perform the operation.

  • (10.2)

    resource_deadlock_would_occur — if the implementation detects that a deadlock would occur.

11

#

The expression m.unlock_shared() is well-formed and has the following semantics:

12

#

Preconditions: The calling thread holds a shared lock on the mutex.

13

#

Effects: Releases a shared lock on the mutex held by the calling thread.

14

#

Return type: void.

15

#

Synchronization: This operation synchronizes with subsequentlock() operations that obtain ownership on the same object.

16

#

Throws: Nothing.

17

#

The expression m.try_lock_shared() is well-formed and has the following semantics:

18

#

Preconditions: The calling thread has no ownership of the mutex.

19

#

Effects: Attempts to obtain shared ownership of the mutex for the calling thread without blocking.

If shared ownership is not obtained, there is no effect and try_lock_shared() immediately returns.

An implementation may fail to obtain the lock even if it is not held by any other thread.

20

#

Synchronization: If try_lock_shared() returns true, prior unlock() operations on the same object synchronize with ([intro.multithread]) this operation.

21

#

Return type: bool.

22

#

Returns: true if the shared lock was acquired, otherwise false.

23

#

Throws: Nothing.

32.6.4.4.2 Class shared_mutex [thread.sharedmutex.class]

🔗

namespace std {class shared_mutex {public: shared_mutex(); ~shared_mutex();

shared_mutex(const shared_mutex&) = delete; shared_mutex& operator=(const shared_mutex&) = delete; // exclusive ownershipvoid lock(); // blockingbool try_lock(); void unlock(); // shared ownershipvoid lock_shared(); // blockingbool try_lock_shared(); void unlock_shared(); using native_handle_type = implementation-defined; // see [thread.req.native] native_handle_type native_handle(); // see [thread.req.native]};}

1

#

The class shared_mutex provides a non-recursive mutex with shared ownership semantics.

2

#

The class shared_mutex meets all of the shared mutex requirements ([thread.sharedmutex.requirements]).

It is a standard-layout class ([class.prop]).

3

#

The behavior of a program is undefined if

it destroys a shared_mutex object owned by any thread,

a thread attempts to recursively gain any ownership of a shared_mutex, or

a thread terminates while possessing any ownership of a shared_mutex.

4

#

shared_mutex may be a synonym for shared_timed_mutex.