CsLibGuarded  1.4.2
libguarded::lr_guarded< T, Mutex > Class Template Reference

#include <CsLibGuarded/cs_lr_guarded.h>

Public Member Functions

template<typename... Us>
 lr_guarded (Us &&... data)
 
template<typename Func>
void modify (Func &&f)
 
shared_handle lock_shared () const
 
shared_handle try_lock_shared () const
 
template<class Duration>
shared_handle try_lock_shared_for (const Duration &duration) const
 
template<class TimePoint>
shared_handle try_lock_shared_until (const TimePoint &timepoint) const
 

Detailed Description

template<typename T, typename Mutex = std::mutex>
class libguarded::lr_guarded< T, Mutex >

This templated class wraps an object and allows only one thread at a time to modify the protected object.

This class will use std::mutex for the internal locking mechanism by default. Other classes which are useful for the mutex type are std::recursive_mutex, std::timed_mutex, and std::recursive_timed_mutex.

The handle returned by the various lock methods is moveable and copyable.

Internally this class operates by maintaining two copies of the data and applying the same modifications to each copy in turn. Therefore the lr_guarded object will consume twice as much memory as one T plus a small amount of overhead.

The T class must be copy constructible and copy assignable.

Definition at line 51 of file cs_lr_guarded.h.

Constructor & Destructor Documentation

template<typename T, typename Mutex = std::mutex>
template<typename... Us>
libguarded::lr_guarded< T, Mutex >::lr_guarded ( Us &&...   data)

Construct an lr_guarded object. This constructor will accept any number of parameters, all of which are forwarded to the constructor of T.

Definition at line 64 of file cs_lr_guarded.h.

Member Function Documentation

template<typename T, typename Mutex = std::mutex>
shared_handle libguarded::lr_guarded< T, Mutex >::lock_shared ( ) const
nodiscard

Acquire a shared_handle to the protected object. Always succeeds without blocking.

Definition at line 84 of file cs_lr_guarded.h.

template<typename T, typename Mutex = std::mutex>
template<typename Func>
void libguarded::lr_guarded< T, Mutex >::modify ( Func &&  f)

Modify the data by passing a functor. The functor must take exactly one argument of type T&. The functor will be called twice, once for each copy of the data. It must make the same modification for both invocations.

If the first invocation of the functor throws an exception, the modification will be rolled back by copying from the unchanged copy. If the second invocation throws, the modification will be completed by copying from the changed copy. In either case if the copy constructor throws, the data is left in an indeterminate state.

Definition at line 79 of file cs_lr_guarded.h.

template<typename T, typename Mutex = std::mutex>
shared_handle libguarded::lr_guarded< T, Mutex >::try_lock_shared ( ) const
nodiscard

Acquire a shared_handle to the protected object. Always succeeds without blocking.

Definition at line 89 of file cs_lr_guarded.h.

template<typename T, typename Mutex = std::mutex>
template<class Duration>
shared_handle libguarded::lr_guarded< T, Mutex >::try_lock_shared_for ( const Duration &  duration) const
nodiscard

Acquire a shared_handle to the protected object. Always succeeds without blocking.

Definition at line 95 of file cs_lr_guarded.h.

template<typename T, typename Mutex = std::mutex>
template<class TimePoint>
shared_handle libguarded::lr_guarded< T, Mutex >::try_lock_shared_until ( const TimePoint &  timepoint) const
nodiscard

Acquire a shared_handle to the protected object. Always succeeds without blocking.

Definition at line 101 of file cs_lr_guarded.h.


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