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

#include <CsLibGuarded/cs_cow_guarded.h>

Classes

class  handle
 

Public Member Functions

template<typename... Us>
 cow_guarded (Us &&... data)
 
handle lock ()
 
handle try_lock ()
 
template<class Duration>
handle try_lock_for (const Duration &duration)
 
template<class TimePoint>
handle try_lock_until (const TimePoint &timepoint)
 
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::cow_guarded< T, Mutex >

This templated class wraps an object and allows only one thread at a time to modify the protected object. Any number of threads can read the protected object simultaneously and the version as of the time of reading will be maintained until no longer needed.

When a thread locks the cow_guarded object for writing, a copy is made so that the writer may modify the stored data without race conditions. When the handle is released, the old copy is atomically replaced by the modified copy.

The handle type supports a cancel() method, which will discard the modified copy and immediately unlock the data without applying the changes.

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 but not copyable. The shared_handle type is moveable and copyable.

The T class must be copy constructible.

Definition at line 58 of file cs_cow_guarded.h.

Constructor & Destructor Documentation

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

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

Definition at line 73 of file cs_cow_guarded.h.

Member Function Documentation

template<typename T, typename Mutex = std::mutex>
handle libguarded::cow_guarded< T, Mutex >::lock ( )

Acquire a handle to the protected object. As a side effect, the protected object will be locked from access by any other thread. The lock will be automatically released when the handle is destroyed.

Definition at line 81 of file cs_cow_guarded.h.

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

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

Definition at line 129 of file cs_cow_guarded.h.

template<typename T, typename Mutex = std::mutex>
handle libguarded::cow_guarded< T, Mutex >::try_lock ( )

Attempt to acquire a handle to the protected object. Returns a null handle if the object is already locked. As a side effect, the protected object will be locked from access by any other thread. The lock will be automatically released when the handle is destroyed.

Definition at line 90 of file cs_cow_guarded.h.

template<typename T, typename Mutex = std::mutex>
template<class Duration>
handle libguarded::cow_guarded< T, Mutex >::try_lock_for ( const Duration &  duration)

Attempt to acquire a handle to the protected object. As a side effect, the protected object will be locked from access by any other thread. The lock will be automatically released when the handle is destroyed.

Returns a null handle if the object is already locked, and does not become available for locking before the time duration has elapsed.

Calling this method requires that the underlying mutex type M supports the try_lock_for method. This is not true if M is the default std::mutex.

Definition at line 107 of file cs_cow_guarded.h.

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

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

Definition at line 135 of file cs_cow_guarded.h.

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

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

Definition at line 142 of file cs_cow_guarded.h.

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

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

Definition at line 149 of file cs_cow_guarded.h.

template<typename T, typename Mutex = std::mutex>
template<class TimePoint>
handle libguarded::cow_guarded< T, Mutex >::try_lock_until ( const TimePoint &  timepoint)

Attempt to acquire a handle to the protected object. As a side effect, the protected object will be locked from access by any other thread. The lock will be automatically released when the handle is destroyed.

Returns a null handle if the object is already locked, and does not become available for locking before reaching the specified timepoint.

Calling this method requires that the underlying mutex type M supports the try_lock_until method. This is not true if M is the default std::mutex.

Definition at line 123 of file cs_cow_guarded.h.


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