CppUtils::Thread::Accessor<const T, Mutex>
CLASSCppUtils::Thread::Accessor<class T, class Mutex = std::mutex>RAII accessor providing exclusive read-only access to a const-qualified value.
Description
Section intitulée « Description »Holds an exclusive lock (std::unique_lock) preventing concurrent access by other threads, but restricts operations on the protected value to const member functions and read-only inspection.
| Paramètre template | Description |
|---|---|
T | The type of the value being accessed (const-qualified). |
Mutex | The mutex type used for synchronization. |
Méthodes et fonctions publiques
Section intitulée « Méthodes et fonctions publiques »Constructs a const accessor by acquiring a unique lock on the provided mutex.
| Paramètre | Description |
|---|---|
mutex | Mutex to lock. |
value | Const reference to the value to protect. |
[[nodiscard]] constexpr auto Accessor::value(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> decltype(auto);Returns a const reference to the protected value.
[[nodiscard]] constexpr auto Accessor::operator->(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> auto*;Accesses const 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 const 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 = state.access();condition.wait(accessor.getLockGuard(), [&] { return accessor->isReady();});Retourne : Reference to the internal std::unique_lock<Mutex>.