CppUtils::Thread::ReadOnlyAccessor<class T, class Mutex = SharedMutex>
CLASSRAII accessor providing concurrent read-only access to a protected value.
Description
Section intitulée « Description »Uses std::shared_lock internally to allow multiple readers simultaneously.
| Paramètre template | Description |
|---|---|
T | Type of the value being accessed. |
Mutex | Mutex type used for locking (defaults to SharedMutex). |
Méthodes et fonctions publiques
Section intitulée « Méthodes et fonctions publiques »Constructs a read-only accessor by acquiring a shared lock on the mutex.
| Paramètre | Description |
|---|---|
mutex | Shared mutex to lock. |
value | Reference 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");});Retourne : Reference to the internal std::shared_lock<Mutex>.