Sen API
Sen Libraries
Loading...
Searching...
No Matches
sen::Guarded< T, MutexType > Struct Template Reference

Guarded is a lightweight wrapper class that can be used to protect an objet of type T from concurrent modification. The wrapper only allows access/reads/modifications of the underlying value while holding a lock but does not require the user to hold the lock himself. More...

#include <mutex_utils.h>

Classes

struct  TemporaryAccessToken
 Access token that enables the user to interact with the protected object while simultaneously holding a lock to ensure access are correctly synchronized. More...

Public Types

using UnderlyingValueTypeOfT = std::remove_reference_t<T>

Public Member Functions

template<typename... TArgs>
 Guarded (TArgs... args)
UnderlyingValueTypeOfT getValue () const
 Returns a copy of the protected value.
 operator UnderlyingValueTypeOfT () const
template<typename U, std::enable_if_t< std::is_convertible_v< U, T >, bool > = true>
Guardedoperator= (U &&newValue)
template<typename U>
operator+ (U &&rhs) const
template<typename U>
Guardedoperator+= (U &&rhs)
TemporaryAccessToken operator-> ()
TemporaryAccessToken createAccessToken ()
template<typename CallableType>
auto invoke (CallableType callable)
 Immediately invokes the given callable while holding a block, passing in the protected value.

Friends

template<typename LHSType, typename RHSType, std::enable_if_t< std::disjunction_v< std::is_same< LHSType, Guarded >, std::is_same< RHSType, Guarded > >, bool > = true>
bool operator== (const LHSType &lhs, const RHSType &rhs) noexcept
template<typename LHSType, typename RHSType, std::enable_if_t< std::disjunction_v< std::is_same< LHSType, Guarded >, std::is_same< RHSType, Guarded > >, bool > = true>
bool operator!= (const LHSType &lhs, const RHSType &rhs) noexcept

Detailed Description

template<typename T, typename MutexType = std::mutex>
struct sen::Guarded< T, MutexType >

Guarded is a lightweight wrapper class that can be used to protect an objet of type T from concurrent modification. The wrapper only allows access/reads/modifications of the underlying value while holding a lock but does not require the user to hold the lock himself.

Example:

struct MyClass
{
void add(int val)
{
protectedInt += val; // automatically holds a lock while modifying the protected int
}
Guarded<int> protectedInt;
};
Guarded is a lightweight wrapper class that can be used to protect an objet of type T from concurrent...
Definition mutex_utils.h:49
Note
Guarded cannot guarantee that no lock-order inversion occurs, so even with the wrapper the developer still needs to ensure that concurrent reads/writes do not access different wrappers or other mutexes in different orders.

Furthermore, keep in mind that the best concurrent code is the one that is designed in a way to work without locked values.

Template Parameters
Tthe type of the underlying protected value
MutexTypethe type of the mutex that should be used

Member Typedef Documentation

◆ UnderlyingValueTypeOfT

template<typename T, typename MutexType = std::mutex>
using sen::Guarded< T, MutexType >::UnderlyingValueTypeOfT = std::remove_reference_t<T>

Constructor & Destructor Documentation

◆ Guarded()

template<typename T, typename MutexType = std::mutex>
template<typename... TArgs>
sen::Guarded< T, MutexType >::Guarded ( TArgs... args)
inlineexplicit

Member Function Documentation

◆ getValue()

template<typename T, typename MutexType = std::mutex>
UnderlyingValueTypeOfT sen::Guarded< T, MutexType >::getValue ( ) const
inline

Returns a copy of the protected value.

◆ operator UnderlyingValueTypeOfT()

template<typename T, typename MutexType = std::mutex>
sen::Guarded< T, MutexType >::operator UnderlyingValueTypeOfT ( ) const
inline

◆ operator=()

template<typename T, typename MutexType = std::mutex>
template<typename U, std::enable_if_t< std::is_convertible_v< U, T >, bool > = true>
Guarded & sen::Guarded< T, MutexType >::operator= ( U && newValue)
inline

◆ operator+()

template<typename T, typename MutexType = std::mutex>
template<typename U>
T sen::Guarded< T, MutexType >::operator+ ( U && rhs) const
inline

◆ operator+=()

template<typename T, typename MutexType = std::mutex>
template<typename U>
Guarded & sen::Guarded< T, MutexType >::operator+= ( U && rhs)
inline

◆ operator->()

template<typename T, typename MutexType = std::mutex>
TemporaryAccessToken sen::Guarded< T, MutexType >::operator-> ( )
inline

◆ createAccessToken()

template<typename T, typename MutexType = std::mutex>
TemporaryAccessToken sen::Guarded< T, MutexType >::createAccessToken ( )
inlinenodiscard

◆ invoke()

template<typename T, typename MutexType = std::mutex>
template<typename CallableType>
auto sen::Guarded< T, MutexType >::invoke ( CallableType callable)
inline

Immediately invokes the given callable while holding a block, passing in the protected value.

Parameters
callablethat will be invoke with the protected value

◆ operator==

template<typename T, typename MutexType = std::mutex>
template<typename LHSType, typename RHSType, std::enable_if_t< std::disjunction_v< std::is_same< LHSType, Guarded >, std::is_same< RHSType, Guarded > >, bool > = true>
bool operator== ( const LHSType & lhs,
const RHSType & rhs )
friend

◆ operator!=

template<typename T, typename MutexType = std::mutex>
template<typename LHSType, typename RHSType, std::enable_if_t< std::disjunction_v< std::is_same< LHSType, Guarded >, std::is_same< RHSType, Guarded > >, bool > = true>
bool operator!= ( const LHSType & lhs,
const RHSType & rhs )
friend

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