Skip to content
Welcome to the new CppUtils documentation!

RAII accessor providing concurrent read-only access to a protected value.

Uses std::shared_lock internally to allow multiple readers simultaneously.

Template parameterDescription
TType of the value being accessed.
MutexMutex type used for locking (defaults to SharedMutex).
explicit constexpr ReadOnlyAccessor::ReadOnlyAccessor(Mutex& mutex, const T& value);

Constructs a read-only accessor by acquiring a shared lock on the mutex.

ParameterDescription
mutexShared mutex to lock.
valueReference to the value to read.

[[nodiscard]] constexpr auto ReadOnlyAccessor::value(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> decltype(auto);

Returns a const reference to the protected value.


[[nodiscard]] constexpr auto ReadOnlyAccessor::operator->(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> const T*;

Accesses const member functions or fields of the protected value.


[[nodiscard]] constexpr auto ReadOnlyAccessor::operator*(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> decltype(auto);

Dereferences to obtain the protected const value (equivalent to calling value()).


[[nodiscard]] constexpr auto ReadOnlyAccessor::getLockGuard(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> auto&;

Returns a reference to the underlying std::shared_lock guard.

Provides direct access to the lock guard, allowing it to be used with synchronization primitives requiring a std::shared_lock (such as std::condition_variable_any::wait).

When used with std::condition_variable_any::wait, the wait function atomically releases the shared mutex while placing the thread to sleep, and re-acquires the shared lock upon waking before returning once the predicate evaluates to true:

auto reader = cache.sharedAccess();
conditionAny.wait(reader.getLockGuard(), [&] {
return reader->contains("ready");
});

Returns: Reference to the internal std::shared_lock<Mutex>.