Skip to content
Welcome to the new CppUtils documentation!

RAII accessor providing exclusive read and write access to a protected value.

Does not own the value, only holds a reference and manages the underlying lock guard.

Template parameterDescription
TThe type of the value being accessed.
MutexThe mutex type used for synchronization (defaults to std::mutex).
explicit constexpr Accessor::Accessor(Mutex& mutex, T& value);

Constructs an accessor by acquiring a unique lock on the provided mutex.

ParameterDescription
mutexMutex to lock.
valueReference to the value to protect.

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

Returns a reference to the protected value.


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

Accesses member functions or fields of the protected value.


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

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


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

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

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

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

auto accessor = queue.access();
condition.wait(accessor.getLockGuard(), [&] {
return not std::ranges::empty(accessor.value());
});
auto item = accessor->pop();

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