carb::thread::shared_mutex

Defined in carb/thread/SharedMutex.h

class shared_mutex : private detail::SharedMutexBase

A shared mutex implementation conforming to C++17’s shared_mutex.

This implementation is non-recursive. See carb::thread::recursive_shared_mutex if a recursive shared_mutex is desired.

Note

The underlying implementations are based on Slim Reader/Writer (SRW) Locks for Windows, and POSIX read/write lock objects on Linux.

Subclassed by carb::thread::recursive_shared_mutex

Public Functions

constexpr shared_mutex() = default

Constructor.

~shared_mutex() = default

Destructor.

Debug builds assert that the mutex is not busy (locked) when destroyed.

inline void lock()

Blocks until an exclusive lock can be obtained.

When this function returns, the calling thread exclusively owns the mutex. At some later point the calling thread will need to call unlock() to release the exclusive lock.

Warning

Debug builds will assert that the calling thread does not already own the lock.

inline bool try_lock()

Attempts to immediately take an exclusive lock, but will not block if one cannot be obtained.

Warning

Debug builds will assert that the calling thread does not already own the lock.

Returns

true if an exclusive lock could be obtained, and at some later point unlock() will need to be called to release the lock. If an exclusive lock could not be obtained immediately, false is returned.

inline void unlock()

Releases an exclusive lock held by the calling thread.

Warning

Debug builds will assert that the calling thread owns the lock exclusively.

inline void lock_shared()

Blocks until a shared lock can be obtained.

When this function returns, the calling thread has obtained a shared lock on the resources protected by the mutex. At some later point the calling thread must call unlock_shared() to release the shared lock.

Warning

Debug builds will assert that the calling thread does not already own the lock.

inline bool try_lock_shared()

Attempts to immediately take a shared lock, but will not block if one cannot be obtained.

Warning

Debug builds will assert that the calling thread does not already own the lock.

Returns

true if a shared lock could be obtained, and at some later point unlock_shared() will need to be called to release the lock. If a shared lock could not be obtained immediately, false is returned.

inline void unlock_shared()

Releases a shared lock held by the calling thread.

Warning

Debug builds will assert that the calling thread owns a shared lock.