CppUtils::Thread::Accessor<class T, class Mutex = std::mutex>
CLASSRAII accessor providing exclusive read and write access to a protected value.
Description
Section titled “Description”Does not own the value, only holds a reference and manages the underlying lock guard.
| Template parameter | Description |
|---|---|
T | The type of the value being accessed. |
Mutex | The mutex type used for synchronization (defaults to std::mutex). |
Public methods & functions
Section titled “Public methods & functions”Constructs an accessor by acquiring a unique lock on the provided mutex.
| Parameter | Description |
|---|---|
mutex | Mutex to lock. |
value | Reference 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>.