mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
mrs_lib::OwningMutex< T, Mutex > Class Template Reference

A thread-safe wrapper for shared data with mutex protection. More...

#include <owning_mutex.hpp>

Public Member Functions

 OwningMutex ()=default
 Construct with default-initialized data.
 OwningMutex (const T &val)
 Construct by copying the data.
 OwningMutex (T &&val)
 Construct by moving the data.
 ~OwningMutex ()=default
 Default destructor.
 OwningMutex (const OwningMutex &)=delete
 OwningMutex is immovable.
OwningMutex & operator= (const OwningMutex &)=delete
 OwningMutex is immovable.
 OwningMutex (OwningMutex &&)=delete
 OwningMutex is immovable.
OwningMutex & operator= (OwningMutex &&)=delete
 OwningMutex is immovable.
OwningMutexGuard< true > acquire ()
 Acquires a mutable guard for the OwningMutex.
OwningMutexGuard< false > acquire () const
 Acquires a const guard for the OwningMutex.
T load () const
 Loads the current value of the data.
void store (T val)
 Stores a new value into the data.

Detailed Description

template<typename T, typename Mutex = std::mutex>
requires (std::same_as<T, std::remove_cvref_t<T>> && std::default_initializable<Mutex>)
class mrs_lib::OwningMutex< T, Mutex >

A thread-safe wrapper for shared data with mutex protection.

Template Parameters
TThe type of data to be protected by the mutex.
MutexThe mutex type to use (defaults to std::mutex).

Constructor & Destructor Documentation

◆ OwningMutex() [1/3]

template<typename T, typename Mutex = std::mutex>
mrs_lib::OwningMutex< T, Mutex >::OwningMutex ( )
default

Construct with default-initialized data.

Note
This constructor is only available if the type of the contained object is default constructible.

◆ OwningMutex() [2/3]

template<typename T, typename Mutex = std::mutex>
mrs_lib::OwningMutex< T, Mutex >::OwningMutex ( const T & val)
inline

Construct by copying the data.

Parameters
valThe value to copy into the OwningMutex.

◆ OwningMutex() [3/3]

template<typename T, typename Mutex = std::mutex>
mrs_lib::OwningMutex< T, Mutex >::OwningMutex ( T && val)
inline

Construct by moving the data.

Parameters
valThe value to move into the OwningMutex.

Member Function Documentation

◆ acquire() [1/2]

template<typename T, typename Mutex = std::mutex>
OwningMutexGuard< true > mrs_lib::OwningMutex< T, Mutex >::acquire ( )
inline

Acquires a mutable guard for the OwningMutex.

Returns
A mutable guard that allows modification of the data.

◆ acquire() [2/2]

template<typename T, typename Mutex = std::mutex>
OwningMutexGuard< false > mrs_lib::OwningMutex< T, Mutex >::acquire ( ) const
inline

Acquires a const guard for the OwningMutex.

Returns
A const guard that allows read-only access to the data.

◆ load()

template<typename T, typename Mutex = std::mutex>
T mrs_lib::OwningMutex< T, Mutex >::load ( ) const
inlinenodiscard

Loads the current value of the data.

Returns
The current value of the data.

◆ store()

template<typename T, typename Mutex = std::mutex>
void mrs_lib::OwningMutex< T, Mutex >::store ( T val)
inline

Stores a new value into the data.

Parameters
valThe value to store.
Note
This method requires the type of the contained class to be noexcept move constructible.

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