libbio
Loading...
Searching...
No Matches
Public Member Functions | List of all members
bio::SafelyAccessShared< CLASS > Class Template Reference

#include <SafelyAccessShared.h>

Public Member Functions

 SafelyAccessShared ()
 
 SafelyAccessShared (Pointer< CLASS > toAccess)
 
 ~SafelyAccessShared ()
 
Pointer< const CLASSoperator-> () const
 
Pointer< const CLASSoperator* () const
 

Detailed Description

template<class CLASS>
class bio::SafelyAccessShared< CLASS >

RAII reader-side companion to SafelyAccess<T>. Acquires the shared (reader) side of the underlying ThreadSafe object's mutex on construction; releases on destruction. Multiple SafelyAccessShared holders may proceed in parallel; an in-flight SafelyAccess<T> (exclusive) blocks all SafelyAccessShared acquisitions and vice-versa.

Use case: read-mostly hot paths on Perspective / IdPerspective / TypedPerspective and other ThreadSafe derivatives where the existing exclusive lock serialises uncontested lookups. The classic shape:

{
    SafelyAccessShared<Perspective<X>> shared(Pointer<Perspective<X> >(&perspective));
    DIMENSION existing = shared->GetIdWithoutCreation(name);
    if (existing) return existing;
}
{
    // Miss: drop shared, take exclusive, recheck (another thread
    // may have inserted between our shared release and exclusive
    // acquire), then insert.
    SafelyAccess<Perspective<X>> exclusive(Pointer<Perspective<X> >(&perspective));
    DIMENSION existing = exclusive->GetIdWithoutCreation(name);
    if (existing) return existing;
    return exclusive->GetIdFromName(name);
}

Caveats:

Template Parameters
CLASSa child of bio::ThreadSafe.

Constructor & Destructor Documentation

◆ SafelyAccessShared() [1/2]

template<class CLASS >
bio::SafelyAccessShared< CLASS >::SafelyAccessShared ( )
inline

Constructor for Singletons. Acquires shared lock on CLASS::Instance().

◆ SafelyAccessShared() [2/2]

template<class CLASS >
bio::SafelyAccessShared< CLASS >::SafelyAccessShared ( Pointer< CLASS toAccess)
inline

Constructor for explicit instances.

Parameters
toAccessthe ThreadSafe object to lock for reading.

◆ ~SafelyAccessShared()

Release RAII shared lock.

Member Function Documentation

◆ operator*()

template<class CLASS >
Pointer< const CLASS > bio::SafelyAccessShared< CLASS >::operator* ( ) const
inline

Const access to the locked object.

Returns
a const Pointer to the locked CLASS.

◆ operator->()

template<class CLASS >
Pointer< const CLASS > bio::SafelyAccessShared< CLASS >::operator-> ( ) const
inline

Const access to the locked object. Non-const methods are unreachable via this overload by design — calling a mutating method while holding only a shared lock would race with other shared holders.

Returns
a const Pointer to the locked CLASS.

The documentation for this class was generated from the following file: