Mutexed 0.0.1
A header-only mutex-protected value wrapper for C++20.
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | List of all members
llh::mutexed::Mutexed< T, M, H > Class Template Reference

The Mutexed class is a value-wrapper that protects its value with a mutex that will be referred to in this documentation as the inner mutex. More...

#include <mutexed.hpp>

+ Inheritance diagram for llh::mutexed::Mutexed< T, M, H >:
+ Collaboration diagram for llh::mutexed::Mutexed< T, M, H >:

Public Types

using value_type = T
 The type of the wrapped value.
 
using mutex_type = M
 The type of the inner mutex
 
using possibly_shared_lock = std::conditional_t< shared_lockable< M >, std::shared_lock< M >, std::unique_lock< M > >
 A std::shared_lock<M> if Mutexed::mutex_type is shared_lockable , a std::unique_lock<M> otherwise.
 

Public Member Functions

 Mutexed (Mutexed &&)=delete
 
 Mutexed (Mutexed const &)=delete
 
template<typename... ValueArgs>
requires does_not_contain_tag<mutex_args_t, ValueArgs...> && std::is_constructible_v<T, ValueArgs&&...>
 Mutexed (ValueArgs &&... args)
 In-place-constructs the wrapped value with the provided arguments and default-initializes the mutex.
 
template<typename ValArg , typename MutexArg >
 Mutexed (ValArg &&v_arg, MutexArg &&m_arg)
 Forwards the first argument to the constructor of the value and the second argument to the constructor of the mutex.
 
template<typename... MutexArgs>
requires does_not_contain_tag<value_args_t, MutexArgs...>
 Mutexed (mutex_args_t, MutexArgs &&... m_args)
 In-place-constructs the mutex with the provided arguments and default-initializes the wrapped value.
 
template<typename F >
requires invokable_with<F, T const&> || invokable_with<F, T> && std::is_copy_constructible_v<T>
decltype(auto) with_locked (F &&f) const
 Calls f with a const& or a copy of the wrapped value while locking the inner mutex.
 
template<typename F >
requires invokable_with<F, T&>
decltype(auto) with_locked (F &&f)
 Calls f with a reference on the wrapped value while locking the inner mutex.
 
template<typename = void>
requires std::is_copy_constructible_v<T>
get_copy () const
 Gets a copy of the wrapped value while locking the inner mutex.
 
template<typename Predicate >
requires std::is_same_v<H, has_cv> && invokable_with<Predicate, T const&>
void wait (Predicate &&p) const
 Waits until this is notified and the provided predicate returns true.
 
template<class Rep , class Period , typename Predicate >
requires std::is_same_v<H, has_cv> && invokable_with<Predicate, T const&>
bool wait_for (std::chrono::duration< Rep, Period > const &rel_time, Predicate &&p) const
 Waits until this is notified and the provided predicate returns true or until the specified duration has been spent waiting.
 
template<class Clock , class Duration , typename Predicate >
requires std::is_same_v<H, has_cv> && invokable_with<Predicate, T const&>
bool wait_until (std::chrono::time_point< Clock, Duration > const &timeout_time, Predicate &&p) const
 Waits until this is notified and the provided predicate returns true or until the specified time point has been passed.
 
decltype(auto) locked ()
 Provides access to the wrapped value through a tuple of an unspecified lock guard and a reference to the value.
 
std::tuple< possibly_shared_lock, T const & > locked () const
 Same as locked_const().
 
std::tuple< possibly_shared_lock, T const & > locked_const () const
 Provides const access to the wrapped value through a tuple of a possibly_shared_lock and a const reference to the wrapped value.
 

Detailed Description

template<typename T, typename M = std::shared_mutex, typename H = no_cv>
class llh::mutexed::Mutexed< T, M, H >

The Mutexed class is a value-wrapper that protects its value with a mutex that will be referred to in this documentation as the inner mutex.

Template Parameters
Tthe type of the wrapped value.
Mthe type of the inner mutex. If it is shared_lockable , read-access to the wrapped value is done by using the lock_shared() function of the inner mutex.
Hoption to activate The waiting feature if it is has_cv. The default value is no_cv, in which case no condition-variable is held and waiting functions are not available.

Definition at line 162 of file mutexed.hpp.

Member Typedef Documentation

◆ mutex_type

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
using llh::mutexed::Mutexed< T, M, H >::mutex_type = M

The type of the inner mutex

Definition at line 197 of file mutexed.hpp.

◆ possibly_shared_lock

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
using llh::mutexed::Mutexed< T, M, H >::possibly_shared_lock = std::conditional_t< shared_lockable<M>, std::shared_lock<M>, std::unique_lock<M> >

A std::shared_lock<M> if Mutexed::mutex_type is shared_lockable , a std::unique_lock<M> otherwise.

Definition at line 202 of file mutexed.hpp.

◆ value_type

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
using llh::mutexed::Mutexed< T, M, H >::value_type = T

The type of the wrapped value.

Definition at line 195 of file mutexed.hpp.

Constructor & Destructor Documentation

◆ Mutexed() [1/3]

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
template<typename... ValueArgs>
requires does_not_contain_tag<mutex_args_t, ValueArgs...> && std::is_constructible_v<T, ValueArgs&&...>
llh::mutexed::Mutexed< T, M, H >::Mutexed ( ValueArgs &&...  args)
inlineexplicit

In-place-constructs the wrapped value with the provided arguments and default-initializes the mutex.

Definition at line 216 of file mutexed.hpp.

◆ Mutexed() [2/3]

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
template<typename ValArg , typename MutexArg >
llh::mutexed::Mutexed< T, M, H >::Mutexed ( ValArg &&  v_arg,
MutexArg &&  m_arg 
)
inlineexplicit

Forwards the first argument to the constructor of the value and the second argument to the constructor of the mutex.

Definition at line 221 of file mutexed.hpp.

◆ Mutexed() [3/3]

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
template<typename... MutexArgs>
requires does_not_contain_tag<value_args_t, MutexArgs...>
llh::mutexed::Mutexed< T, M, H >::Mutexed ( mutex_args_t  ,
MutexArgs &&...  m_args 
)
inlineexplicit

In-place-constructs the mutex with the provided arguments and default-initializes the wrapped value.

Definition at line 230 of file mutexed.hpp.

Member Function Documentation

◆ get_copy()

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
template<typename = void>
requires std::is_copy_constructible_v<T>
T llh::mutexed::Mutexed< T, M, H >::get_copy ( ) const
inline

Gets a copy of the wrapped value while locking the inner mutex.

If M is shared_lockable , lock_shared() will be used.

Definition at line 307 of file mutexed.hpp.

◆ locked() [1/2]

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
decltype(auto) llh::mutexed::Mutexed< T, M, H >::locked ( )
inline

Provides access to the wrapped value through a tuple of an unspecified lock guard and a reference to the value.

Use it this way :

{
auto [lock, ref] = protected.locked();
ref += 42;
}

This function unique-locks the inner mutex before returning the tuple. The lock-guard returned has a destructor that unlocks the inner mutex and then, if The waiting feature is enabled, notifies the inner condition-variable.

Definition at line 415 of file mutexed.hpp.

◆ locked() [2/2]

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
std::tuple< possibly_shared_lock, T const & > llh::mutexed::Mutexed< T, M, H >::locked ( ) const
inline

Same as locked_const().

Definition at line 441 of file mutexed.hpp.

◆ locked_const()

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
std::tuple< possibly_shared_lock, T const & > llh::mutexed::Mutexed< T, M, H >::locked_const ( ) const
inline

Provides const access to the wrapped value through a tuple of a possibly_shared_lock and a const reference to the wrapped value.

Use it this way :

{
auto const [lock, ref] = protected.locked_const();
std::cout << ref;
}

This function locks the inner mutex before returning the tuple. The locking is shared if that mutex is shared_lockable , and regular (lock() used) otherwise.

The lock guard returned has a destructor that unlocks the inner mutex.

Definition at line 463 of file mutexed.hpp.

◆ with_locked() [1/2]

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
template<typename F >
requires invokable_with<F, T&>
decltype(auto) llh::mutexed::Mutexed< T, M, H >::with_locked ( F &&  f)
inline

Calls f with a reference on the wrapped value while locking the inner mutex.

If The waiting feature is enabled, the inner condition-variable is notified with notify_all() after the inner mutex is unlocked.

This overload is chosen if this is not const and if f is invokable_with a non-const reference to value_type.

Example usage :

llh::mutexed::Mutexed<int> protected_int(0);
protected_int.with_locked([](int& val){ val += 42; });
The Mutexed class is a value-wrapper that protects its value with a mutex that will be referred to in...
Definition mutexed.hpp:162
Parameters
fThe functor that will be called with a reference to the wrapped value while the inner mutex will be locked.

Definition at line 297 of file mutexed.hpp.

◆ with_locked() [2/2]

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
template<typename F >
requires invokable_with<F, T const&> || invokable_with<F, T> && std::is_copy_constructible_v<T>
decltype(auto) llh::mutexed::Mutexed< T, M, H >::with_locked ( F &&  f) const
inline

Calls f with a const& or a copy of the wrapped value while locking the inner mutex.

If the inner mutex is shared_lockable , lock_shared() will be used.

This overload is chosen if this is const or if f is invokable_with either value_type or a const& to it.

Example usage :

struct read_me {
int val = 42;
int value() const { return val; }
};
// with a const member function :
std::cout << protected_int.with_locked(&read_me::value) << std::endl;
// with a lambda taking a const ref :
std::cout << protected_int.with_locked([](auto const& rm){ return rm.val; }) << std::endl;
// with a lambda taking an auto but from a const Mutexed :
std::cout << std::cref(protected_int).get().with_locked([](auto rm){ return rm.val; }) << std::endl;
decltype(auto) with_locked(F &&f) const
Calls f with a const& or a copy of the wrapped value while locking the inner mutex.
Definition mutexed.hpp:270
Parameters
fThe functor that will be called with the wrapped value while the inner mutex will be locked.

Definition at line 270 of file mutexed.hpp.


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