|
Mutexed 0.0.1
A header-only mutex-protected value wrapper for C++20.
|
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> | |
| 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. | |
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.
| T | the type of the wrapped value. |
| M | the 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. |
| H | option 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.
| using llh::mutexed::Mutexed< T, M, H >::mutex_type = M |
The type of the inner mutex
Definition at line 197 of file mutexed.hpp.
| 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.
| using llh::mutexed::Mutexed< T, M, H >::value_type = T |
The type of the wrapped value.
Definition at line 195 of file mutexed.hpp.
|
inlineexplicit |
In-place-constructs the wrapped value with the provided arguments and default-initializes the mutex.
Definition at line 216 of file mutexed.hpp.
|
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.
|
inlineexplicit |
In-place-constructs the mutex with the provided arguments and default-initializes the wrapped value.
Definition at line 230 of file mutexed.hpp.
|
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.
|
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 :
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.
|
inline |
Same as locked_const().
Definition at line 441 of file mutexed.hpp.
|
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 :
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.
|
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 :
| f | The 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.
|
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 :
| f | The functor that will be called with the wrapped value while the inner mutex will be locked. |
Definition at line 270 of file mutexed.hpp.