Boost  v1.57.0
doxygen for www.boost.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boost::synchronized_value< T, Lockable > Class Template Reference

cloaks a value type and the mutex used to protect it together. More...

#include <synchronized_value.hpp>

Public Types

typedef T value_type
 
typedef Lockable mutex_type
 

Public Member Functions

 synchronized_value ()
 Default constructor. More...
 
 synchronized_value (T const &other)
 Constructor from copy constructible value. More...
 
 synchronized_value (BOOST_THREAD_RV_REF(T) other)
 Move Constructor. More...
 
 synchronized_value (synchronized_value const &rhs)
 Constructor from value type. More...
 
 synchronized_value (BOOST_THREAD_RV_REF(synchronized_value) other)
 Move Constructor from movable value type. More...
 
synchronized_valueoperator= (synchronized_value const &rhs)
 Assignment operator. More...
 
synchronized_valueoperator= (value_type const &val)
 Assignment operator from a T const&. More...
 
T get () const
 Explicit conversion to value type. More...
 
 operator T () const
 Explicit conversion to value type. More...
 
T const & value () const
 value type getter. More...
 
mutex_type const & mutex () const
 mutex getter. More...
 
void swap (synchronized_value &rhs)
 Swap. More...
 
void swap (value_type &rhs)
 Swap with the underlying value type. More...
 
strict_lock_ptr< T, Lockableoperator-> ()
 Essentially calling a method obj->foo(x, y, z) calls the method foo(x, y, z) inside a critical section as long-lived as the call itself. More...
 
const_strict_lock_ptr< T,
Lockable
operator-> () const
 If the synchronized_value object involved is const-qualified, then you'll only be able to call const methods through operator->. More...
 
template<typename F >
boost::result_of< F(value_type &)>
::type 
operator() (BOOST_THREAD_RV_REF(F) fct)
 Call function on a locked block. More...
 
template<typename F >
boost::result_of< F(value_type
const &)>::type 
operator() (BOOST_THREAD_RV_REF(F) fct) const
 
strict_lock_ptr< T, Lockablesynchronize ()
 The synchronize() factory make easier to lock on a scope. More...
 
const_strict_lock_ptr< T,
Lockable
synchronize () const
 
unique_lock_ptr< T, Lockableunique_synchronize ()
 
const_unique_lock_ptr< T,
Lockable
unique_synchronize () const
 
unique_lock_ptr< T, Lockableunique_synchronize (defer_lock_t tag)
 
const_unique_lock_ptr< T,
Lockable
unique_synchronize (defer_lock_t tag) const
 
unique_lock_ptr< T, Lockabledefer_synchronize () BOOST_NOEXCEPT
 
const_unique_lock_ptr< T,
Lockable
defer_synchronize () const BOOST_NOEXCEPT
 
unique_lock_ptr< T, Lockabletry_to_synchronize () BOOST_NOEXCEPT
 
const_unique_lock_ptr< T,
Lockable
try_to_synchronize () const BOOST_NOEXCEPT
 
unique_lock_ptr< T, Lockableadopt_synchronize () BOOST_NOEXCEPT
 
const_unique_lock_ptr< T,
Lockable
adopt_synchronize () const BOOST_NOEXCEPT
 
deref_value operator* ()
 
const_deref_value operator* () const
 
template<typename OStream >
void save (OStream &os) const
 T is OutputStreamable saves the value type on the output stream os. More...
 
template<typename IStream >
void load (IStream &is) const
 T is InputStreamable loads the value type from the input stream is. More...
 
bool operator== (synchronized_value const &rhs) const
 T is EqualityComparable More...
 
bool operator< (synchronized_value const &rhs) const
 T is LessThanComparable More...
 
bool operator> (synchronized_value const &rhs) const
 T is GreaterThanComparable More...
 
bool operator<= (synchronized_value const &rhs) const
 
bool operator>= (synchronized_value const &rhs) const
 
bool operator== (value_type const &rhs) const
 
bool operator!= (value_type const &rhs) const
 
bool operator< (value_type const &rhs) const
 
bool operator<= (value_type const &rhs) const
 
bool operator> (value_type const &rhs) const
 
bool operator>= (value_type const &rhs) const
 

Friends

template<typename... SV>
std::tuple< typename
synchronized_value_strict_lock_ptr
< SV >::type...> 
synchronize (SV &...sv)
 

Detailed Description

template<typename T, typename Lockable = mutex>
class boost::synchronized_value< T, Lockable >

cloaks a value type and the mutex used to protect it together.

Parameters
Tthe value type.
Lockablethe mutex type protecting the value type.

Member Typedef Documentation

template<typename T, typename Lockable = mutex>
typedef Lockable boost::synchronized_value< T, Lockable >::mutex_type
template<typename T, typename Lockable = mutex>
typedef T boost::synchronized_value< T, Lockable >::value_type

Constructor & Destructor Documentation

template<typename T, typename Lockable = mutex>
boost::synchronized_value< T, Lockable >::synchronized_value ( )
inline

Default constructor.

: T is DefaultConstructible

template<typename T, typename Lockable = mutex>
boost::synchronized_value< T, Lockable >::synchronized_value ( T const &  other)
inline

Constructor from copy constructible value.

Requires: T is CopyConstructible

template<typename T, typename Lockable = mutex>
boost::synchronized_value< T, Lockable >::synchronized_value ( BOOST_THREAD_RV_REF(T other)
inline

Move Constructor.

Requires: T is CopyMovable

template<typename T, typename Lockable = mutex>
boost::synchronized_value< T, Lockable >::synchronized_value ( synchronized_value< T, Lockable > const &  rhs)
inline

Constructor from value type.

Requires: T is DefaultConstructible and Assignable Effects: Assigns the value on a scope protected by the mutex of the rhs. The mutex is not copied.

template<typename T, typename Lockable = mutex>
boost::synchronized_value< T, Lockable >::synchronized_value ( BOOST_THREAD_RV_REF(synchronized_value< T, Lockable >)  other)
inline

Move Constructor from movable value type.

References boost::move().

Member Function Documentation

template<typename T, typename Lockable = mutex>
unique_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::adopt_synchronize ( )
inline

References boost::adopt_lock.

template<typename T, typename Lockable = mutex>
const_unique_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::adopt_synchronize ( ) const
inline

References boost::adopt_lock.

template<typename T, typename Lockable = mutex>
unique_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::defer_synchronize ( )
inline

References boost::defer_lock.

template<typename T, typename Lockable = mutex>
const_unique_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::defer_synchronize ( ) const
inline

References boost::defer_lock.

template<typename T, typename Lockable = mutex>
T boost::synchronized_value< T, Lockable >::get ( ) const
inline

Explicit conversion to value type.

Requires: T is CopyConstructible Return: A copy of the protected value obtained on a scope protected by the mutex.

template<typename T, typename Lockable = mutex>
template<typename IStream >
void boost::synchronized_value< T, Lockable >::load ( IStream &  is) const
inline

T is InputStreamable loads the value type from the input stream is.

Referenced by boost::operator>>().

template<typename T, typename Lockable = mutex>
mutex_type const& boost::synchronized_value< T, Lockable >::mutex ( ) const
inline

mutex getter.

Return: A constant reference to the protecting mutex.

Note: Not thread safe

template<typename T, typename Lockable = mutex>
boost::synchronized_value< T, Lockable >::operator T ( ) const
inlineexplicit

Explicit conversion to value type.

Requires: T is CopyConstructible Return: A copy of the protected value obtained on a scope protected by the mutex.

template<typename T, typename Lockable = mutex>
bool boost::synchronized_value< T, Lockable >::operator!= ( value_type const &  rhs) const
inline
template<typename T, typename Lockable = mutex>
template<typename F >
boost::result_of<F(value_type&)>::type boost::synchronized_value< T, Lockable >::operator() ( BOOST_THREAD_RV_REF(F)  fct)
inline

Call function on a locked block.

fct(value_) is well formed.

Example void fun(synchronized_value<vector<int>> & v) { v ( [](vector<int>> & vec) { vec.push_back(42); assert(vec.back() == 42); } ); }

template<typename T, typename Lockable = mutex>
template<typename F >
boost::result_of<F(value_type const&)>::type boost::synchronized_value< T, Lockable >::operator() ( BOOST_THREAD_RV_REF(F)  fct) const
inline
template<typename T, typename Lockable = mutex>
deref_value boost::synchronized_value< T, Lockable >::operator* ( )
inline
template<typename T, typename Lockable = mutex>
const_deref_value boost::synchronized_value< T, Lockable >::operator* ( ) const
inline
template<typename T, typename Lockable = mutex>
strict_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::operator-> ( )
inline

Essentially calling a method obj->foo(x, y, z) calls the method foo(x, y, z) inside a critical section as long-lived as the call itself.

template<typename T, typename Lockable = mutex>
const_strict_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::operator-> ( ) const
inline

If the synchronized_value object involved is const-qualified, then you'll only be able to call const methods through operator->.

So, for example, vec->push_back("xyz") won't work if vec were const-qualified. The locking mechanism capitalizes on the assumption that const methods don't modify their underlying data.

template<typename T, typename Lockable = mutex>
bool boost::synchronized_value< T, Lockable >::operator< ( synchronized_value< T, Lockable > const &  rhs) const
inline

T is LessThanComparable

References boost::defer_lock, and boost::lock().

template<typename T, typename Lockable = mutex>
bool boost::synchronized_value< T, Lockable >::operator< ( value_type const &  rhs) const
inline
template<typename T, typename Lockable = mutex>
bool boost::synchronized_value< T, Lockable >::operator<= ( synchronized_value< T, Lockable > const &  rhs) const
inline

References boost::defer_lock, and boost::lock().

template<typename T, typename Lockable = mutex>
bool boost::synchronized_value< T, Lockable >::operator<= ( value_type const &  rhs) const
inline
template<typename T, typename Lockable = mutex>
synchronized_value& boost::synchronized_value< T, Lockable >::operator= ( synchronized_value< T, Lockable > const &  rhs)
inline

Assignment operator.

Effects: Copies the underlying value on a scope protected by the two mutexes. The mutex is not copied. The locks are acquired using lock, so deadlock is avoided. For example, there is no problem if one thread assigns a = b and the other assigns b = a.

Return: *this

References boost::defer_lock, and boost::lock().

template<typename T, typename Lockable = mutex>
synchronized_value& boost::synchronized_value< T, Lockable >::operator= ( value_type const &  val)
inline

Assignment operator from a T const&.

Effects: The operator copies the value on a scope protected by the mutex. Return: *this

References boost::phoenix::val().

template<typename T, typename Lockable = mutex>
bool boost::synchronized_value< T, Lockable >::operator== ( synchronized_value< T, Lockable > const &  rhs) const
inline

T is EqualityComparable

References boost::defer_lock, and boost::lock().

template<typename T, typename Lockable = mutex>
bool boost::synchronized_value< T, Lockable >::operator== ( value_type const &  rhs) const
inline
template<typename T, typename Lockable = mutex>
bool boost::synchronized_value< T, Lockable >::operator> ( synchronized_value< T, Lockable > const &  rhs) const
inline

T is GreaterThanComparable

References boost::defer_lock, and boost::lock().

template<typename T, typename Lockable = mutex>
bool boost::synchronized_value< T, Lockable >::operator> ( value_type const &  rhs) const
inline
template<typename T, typename Lockable = mutex>
bool boost::synchronized_value< T, Lockable >::operator>= ( synchronized_value< T, Lockable > const &  rhs) const
inline

References boost::defer_lock, and boost::lock().

template<typename T, typename Lockable = mutex>
bool boost::synchronized_value< T, Lockable >::operator>= ( value_type const &  rhs) const
inline
template<typename T, typename Lockable = mutex>
template<typename OStream >
void boost::synchronized_value< T, Lockable >::save ( OStream &  os) const
inline

T is OutputStreamable saves the value type on the output stream os.

Referenced by boost::operator<<().

template<typename T, typename Lockable = mutex>
void boost::synchronized_value< T, Lockable >::swap ( synchronized_value< T, Lockable > &  rhs)
inline

Swap.

Effects: Swaps the data. Again, locks are acquired using lock(). The mutexes are not swapped. A swap method accepts a T& and swaps the data inside a critical section. This is by far the preferred method of changing the guarded datum wholesale because it keeps the lock only for a short time, thus lowering the pressure on the mutex.

References boost::defer_lock, boost::lock(), and boost::swap.

Referenced by boost::swap().

template<typename T, typename Lockable = mutex>
void boost::synchronized_value< T, Lockable >::swap ( value_type rhs)
inline

Swap with the underlying value type.

Effects: Swaps the data on a scope protected by the mutex.

References boost::swap.

template<typename T, typename Lockable = mutex>
strict_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::synchronize ( )
inline

The synchronize() factory make easier to lock on a scope.

As discussed, operator-> can only lock over the duration of a call, so it is insufficient for complex operations. With synchronize() you get to lock the object in a scoped and to directly access the object inside that scope.

Example void fun(synchronized_value<vector<int>> & v) { auto&& vec=v.synchronize(); vec.push_back(42); assert(vec.back() == 42); }

template<typename T, typename Lockable = mutex>
const_strict_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::synchronize ( ) const
inline
template<typename T, typename Lockable = mutex>
unique_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::try_to_synchronize ( )
inline

References boost::try_to_lock.

template<typename T, typename Lockable = mutex>
const_unique_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::try_to_synchronize ( ) const
inline

References boost::try_to_lock.

template<typename T, typename Lockable = mutex>
unique_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::unique_synchronize ( )
inline
template<typename T, typename Lockable = mutex>
const_unique_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::unique_synchronize ( ) const
inline
template<typename T, typename Lockable = mutex>
unique_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::unique_synchronize ( defer_lock_t  tag)
inline
template<typename T, typename Lockable = mutex>
const_unique_lock_ptr<T,Lockable> boost::synchronized_value< T, Lockable >::unique_synchronize ( defer_lock_t  tag) const
inline
template<typename T, typename Lockable = mutex>
T const& boost::synchronized_value< T, Lockable >::value ( ) const
inline

value type getter.

Return: A constant reference to the protected value.

Note: Not thread safe

Friends And Related Function Documentation

template<typename T, typename Lockable = mutex>
template<typename... SV>
std::tuple<typename synchronized_value_strict_lock_ptr<SV>::type ...> synchronize ( SV &...  sv)
friend

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