1 // (C) Copyright 2010 Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
2 // (C) Copyright 2012 Vicente J. Botet Escriba
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_THREAD_SYNCHRONIZED_VALUE_HPP
9 #define BOOST_THREAD_SYNCHRONIZED_VALUE_HPP
11 #include <boost/thread/detail/config.hpp>
13 #include <boost/thread/detail/move.hpp>
14 #include <boost/thread/mutex.hpp>
15 #include <boost/thread/lock_types.hpp>
16 #include <boost/thread/lock_guard.hpp>
17 #include <boost/thread/lock_algorithms.hpp>
18 #include <boost/thread/lock_factories.hpp>
19 #include <boost/thread/strict_lock.hpp>
20 #include <boost/core/swap.hpp>
21 #include <boost/utility/declval.hpp>
22 //#include <boost/type_traits.hpp>
23 //#include <boost/thread/detail/is_nothrow_default_constructible.hpp>
24 //#if ! defined BOOST_NO_CXX11_HDR_TYPE_TRAITS
25 //#include <type_traits>
28 #if ! defined(BOOST_THREAD_NO_SYNCHRONIZE)
29 #include <tuple> // todo change to <boost/tuple.hpp> once Boost.Tuple or Boost.Fusion provides Move semantics on C++98 compilers.
33 #include <boost/utility/result_of.hpp>
35 #include <boost/config/abi_prefix.hpp>
41 * strict lock providing a const pointer access to the synchronized value type.
43 * @param T the value type.
44 * @param Lockable the mutex type protecting the value type.
46 template <typename T, typename Lockable = mutex>
47 class const_strict_lock_ptr
51 typedef Lockable mutex_type;
54 // this should be a strict_lock, but unique_lock is needed to be able to return it.
55 boost::unique_lock<mutex_type> lk_;
59 BOOST_THREAD_MOVABLE_ONLY( const_strict_lock_ptr )
62 * @param value constant reference of the value to protect.
63 * @param mtx reference to the mutex used to protect the value.
64 * @effects locks the mutex @c mtx, stores a reference to it and to the value type @c value.
66 const_strict_lock_ptr(T const& val, Lockable & mtx) :
70 const_strict_lock_ptr(T const& val, Lockable & mtx, adopt_lock_t tag) BOOST_NOEXCEPT :
71 lk_(mtx, tag), value_(val)
76 * @effects takes ownership of the mutex owned by @c other, stores a reference to the mutex and the value type of @c other.
78 const_strict_lock_ptr(BOOST_THREAD_RV_REF(const_strict_lock_ptr) other) BOOST_NOEXCEPT
79 : lk_(boost::move(BOOST_THREAD_RV(other).lk_)),value_(BOOST_THREAD_RV(other).value_)
83 ~const_strict_lock_ptr()
88 * @return a constant pointer to the protected value
90 const T* operator->() const
96 * @return a constant reference to the protected value
98 const T& operator*() const
106 * strict lock providing a pointer access to the synchronized value type.
108 * @param T the value type.
109 * @param Lockable the mutex type protecting the value type.
111 template <typename T, typename Lockable = mutex>
112 class strict_lock_ptr : public const_strict_lock_ptr<T,Lockable>
114 typedef const_strict_lock_ptr<T,Lockable> base_type;
116 BOOST_THREAD_MOVABLE_ONLY( strict_lock_ptr )
119 * @param value reference of the value to protect.
120 * @param mtx reference to the mutex used to protect the value.
121 * @effects locks the mutex @c mtx, stores a reference to it and to the value type @c value.
123 strict_lock_ptr(T & val, Lockable & mtx) :
127 strict_lock_ptr(T & val, Lockable & mtx, adopt_lock_t tag) :
128 base_type(val, mtx, tag)
134 * @effects takes ownership of the mutex owned by @c other, stores a reference to the mutex and the value type of @c other.
136 strict_lock_ptr(BOOST_THREAD_RV_REF(strict_lock_ptr) other)
137 : base_type(boost::move(static_cast<base_type&>(other)))
146 * @return a pointer to the protected value
150 return const_cast<T*>(&this->value_);
154 * @return a reference to the protected value
158 return const_cast<T&>(this->value_);
163 template <typename SV>
164 struct synchronized_value_strict_lock_ptr
166 typedef strict_lock_ptr<typename SV::value_type, typename SV::mutex_type> type;
169 template <typename SV>
170 struct synchronized_value_strict_lock_ptr<const SV>
172 typedef const_strict_lock_ptr<typename SV::value_type, typename SV::mutex_type> type;
175 * unique_lock providing a const pointer access to the synchronized value type.
177 * An object of type const_unique_lock_ptr is a unique_lock that provides a const pointer access to the synchronized value type.
178 * As unique_lock controls the ownership of a lockable object within a scope.
179 * Ownership of the lockable object may be acquired at construction or after construction,
180 * and may be transferred, after acquisition, to another const_unique_lock_ptr object.
181 * Objects of type const_unique_lock_ptr are not copyable but are movable.
182 * The behavior of a program is undefined if the mutex and the value type
183 * pointed do not exist for the entire remaining lifetime of the const_unique_lock_ptr object.
184 * The supplied Mutex type shall meet the BasicLockable requirements.
186 * @note const_unique_lock_ptr<T, Lockable> meets the Lockable requirements.
187 * If Lockable meets the TimedLockable requirements, const_unique_lock_ptr<T,Lockable>
188 * also meets the TimedLockable requirements.
190 * @param T the value type.
191 * @param Lockable the mutex type protecting the value type.
193 template <typename T, typename Lockable = mutex>
194 class const_unique_lock_ptr : public unique_lock<Lockable>
196 typedef unique_lock<Lockable> base_type;
198 typedef T value_type;
199 typedef Lockable mutex_type;
204 BOOST_THREAD_MOVABLE_ONLY(const_unique_lock_ptr)
207 * @param value reference of the value to protect.
208 * @param mtx reference to the mutex used to protect the value.
210 * @requires If mutex_type is not a recursive mutex the calling thread does not own the mutex.
212 * @effects locks the mutex @c mtx, stores a reference to it and to the value type @c value.
214 const_unique_lock_ptr(T const& val, Lockable & mtx)
215 : base_type(mtx), value_(val)
219 * @param value reference of the value to protect.
220 * @param mtx reference to the mutex used to protect the value.
221 * @param tag of type adopt_lock_t used to differentiate the constructor.
222 * @requires The calling thread own the mutex.
223 * @effects stores a reference to it and to the value type @c value taking ownership.
225 const_unique_lock_ptr(T const& val, Lockable & mtx, adopt_lock_t) BOOST_NOEXCEPT
226 : base_type(mtx, adopt_lock), value_(val)
230 * @param value reference of the value to protect.
231 * @param mtx reference to the mutex used to protect the value.
232 * @param tag of type defer_lock_t used to differentiate the constructor.
233 * @effects stores a reference to it and to the value type @c value c.
235 const_unique_lock_ptr(T const& val, Lockable & mtx, defer_lock_t) BOOST_NOEXCEPT
236 : base_type(mtx, defer_lock), value_(val)
240 * @param value reference of the value to protect.
241 * @param mtx reference to the mutex used to protect the value.
242 * @param tag of type try_to_lock_t used to differentiate the constructor.
243 * @requires If mutex_type is not a recursive mutex the calling thread does not own the mutex.
244 * @effects try to lock the mutex @c mtx, stores a reference to it and to the value type @c value.
246 const_unique_lock_ptr(T const& val, Lockable & mtx, try_to_lock_t) BOOST_NOEXCEPT
247 : base_type(mtx, try_to_lock), value_(val)
252 * @effects takes ownership of the mutex owned by @c other, stores a reference to the mutex and the value type of @c other.
254 const_unique_lock_ptr(BOOST_THREAD_RV_REF(const_unique_lock_ptr) other) BOOST_NOEXCEPT
255 : base_type(boost::move(static_cast<base_type&>(other))), value_(BOOST_THREAD_RV(other).value_)
260 * @effects If owns calls unlock() on the owned mutex.
262 ~const_unique_lock_ptr()
267 * @return a constant pointer to the protected value
269 const T* operator->() const
271 BOOST_ASSERT (this->owns_lock());
276 * @return a constant reference to the protected value
278 const T& operator*() const
280 BOOST_ASSERT (this->owns_lock());
287 * unique lock providing a pointer access to the synchronized value type.
289 * @param T the value type.
290 * @param Lockable the mutex type protecting the value type.
292 template <typename T, typename Lockable = mutex>
293 class unique_lock_ptr : public const_unique_lock_ptr<T, Lockable>
295 typedef const_unique_lock_ptr<T, Lockable> base_type;
297 typedef T value_type;
298 typedef Lockable mutex_type;
300 BOOST_THREAD_MOVABLE_ONLY(unique_lock_ptr)
303 * @param value reference of the value to protect.
304 * @param mtx reference to the mutex used to protect the value.
305 * @effects locks the mutex @c mtx, stores a reference to it and to the value type @c value.
307 unique_lock_ptr(T & val, Lockable & mtx)
308 : base_type(val, mtx)
312 * @param value reference of the value to protect.
313 * @param mtx reference to the mutex used to protect the value.
314 * @param tag of type adopt_lock_t used to differentiate the constructor.
315 * @effects stores a reference to it and to the value type @c value taking ownership.
317 unique_lock_ptr(T & value, Lockable & mtx, adopt_lock_t) BOOST_NOEXCEPT
318 : base_type(value, mtx, adopt_lock)
322 * @param value reference of the value to protect.
323 * @param mtx reference to the mutex used to protect the value.
324 * @param tag of type defer_lock_t used to differentiate the constructor.
325 * @effects stores a reference to it and to the value type @c value c.
327 unique_lock_ptr(T & value, Lockable & mtx, defer_lock_t) BOOST_NOEXCEPT
328 : base_type(value, mtx, defer_lock)
332 * @param value reference of the value to protect.
333 * @param mtx reference to the mutex used to protect the value.
334 * @param tag of type try_to_lock_t used to differentiate the constructor.
335 * @effects try to lock the mutex @c mtx, stores a reference to it and to the value type @c value.
337 unique_lock_ptr(T & value, Lockable & mtx, try_to_lock_t) BOOST_NOEXCEPT
338 : base_type(value, mtx, try_to_lock)
343 * @effects takes ownership of the mutex owned by @c other, stores a reference to the mutex and the value type of @c other.
345 unique_lock_ptr(BOOST_THREAD_RV_REF(unique_lock_ptr) other) BOOST_NOEXCEPT
346 : base_type(boost::move(static_cast<base_type&>(other)))
355 * @return a pointer to the protected value
359 BOOST_ASSERT (this->owns_lock());
360 return const_cast<T*>(&this->value_);
364 * @return a reference to the protected value
368 BOOST_ASSERT (this->owns_lock());
369 return const_cast<T&>(this->value_);
375 template <typename SV>
376 struct synchronized_value_unique_lock_ptr
378 typedef unique_lock_ptr<typename SV::value_type, typename SV::mutex_type> type;
381 template <typename SV>
382 struct synchronized_value_unique_lock_ptr<const SV>
384 typedef const_unique_lock_ptr<typename SV::value_type, typename SV::mutex_type> type;
387 * cloaks a value type and the mutex used to protect it together.
388 * @param T the value type.
389 * @param Lockable the mutex type protecting the value type.
391 template <typename T, typename Lockable = mutex>
392 class synchronized_value
395 #if ! defined(BOOST_THREAD_NO_MAKE_UNIQUE_LOCKS)
396 #if ! defined BOOST_NO_CXX11_VARIADIC_TEMPLATES
397 template <typename ...SV>
398 friend std::tuple<typename synchronized_value_strict_lock_ptr<SV>::type ...> synchronize(SV& ...sv);
400 template <typename SV1, typename SV2>
402 typename synchronized_value_strict_lock_ptr<SV1>::type,
403 typename synchronized_value_strict_lock_ptr<SV2>::type
405 synchronize(SV1& sv1, SV2& sv2);
406 template <typename SV1, typename SV2, typename SV3>
408 typename synchronized_value_strict_lock_ptr<SV1>::type,
409 typename synchronized_value_strict_lock_ptr<SV2>::type,
410 typename synchronized_value_strict_lock_ptr<SV3>::type
412 synchronize(SV1& sv1, SV2& sv2, SV3& sv3);
417 typedef T value_type;
418 typedef Lockable mutex_type;
421 mutable mutex_type mtx_;
423 // construction/destruction
425 * Default constructor.
427 * @Requires: T is DefaultConstructible
430 //BOOST_NOEXCEPT_IF(is_nothrow_default_constructible<T>::value)
436 * Constructor from copy constructible value.
438 * Requires: T is CopyConstructible
440 synchronized_value(T const& other)
441 //BOOST_NOEXCEPT_IF(is_nothrow_copy_constructible<T>::value)
449 * Requires: T is CopyMovable
451 synchronized_value(BOOST_THREAD_RV_REF(T) other)
452 //BOOST_NOEXCEPT_IF(is_nothrow_move_constructible<T>::value)
453 : value_(boost::move(other))
458 * Constructor from value type.
460 * Requires: T is DefaultConstructible and Assignable
461 * Effects: Assigns the value on a scope protected by the mutex of the rhs. The mutex is not copied.
463 synchronized_value(synchronized_value const& rhs)
465 strict_lock<mutex_type> lk(rhs.mtx_);
470 * Move Constructor from movable value type
473 synchronized_value(BOOST_THREAD_RV_REF(synchronized_value) other)
475 strict_lock<mutex_type> lk(BOOST_THREAD_RV(other).mtx_);
476 value_= boost::move(BOOST_THREAD_RV(other).value_);
481 * Assignment operator.
483 * Effects: Copies the underlying value on a scope protected by the two mutexes.
484 * The mutex is not copied. The locks are acquired using lock, so deadlock is avoided.
485 * For example, there is no problem if one thread assigns a = b and the other assigns b = a.
490 synchronized_value& operator=(synchronized_value const& rhs)
494 // auto _ = make_unique_locks(mtx_, rhs.mtx_);
495 unique_lock<mutex_type> lk1(mtx_, defer_lock);
496 unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
504 * Assignment operator from a T const&.
505 * Effects: The operator copies the value on a scope protected by the mutex.
508 synchronized_value& operator=(value_type const& val)
511 strict_lock<mutex_type> lk(mtx_);
519 * Explicit conversion to value type.
521 * Requires: T is CopyConstructible
522 * Return: A copy of the protected value obtained on a scope protected by the mutex.
527 strict_lock<mutex_type> lk(mtx_);
531 * Explicit conversion to value type.
533 * Requires: T is CopyConstructible
534 * Return: A copy of the protected value obtained on a scope protected by the mutex.
537 #if ! defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
538 explicit operator T() const
547 * Return: A constant reference to the protected value.
549 * Note: Not thread safe
552 T const& value() const
559 * Return: A constant reference to the protecting mutex.
561 * Note: Not thread safe
564 mutex_type const& mutex() const
571 * Effects: Swaps the data. Again, locks are acquired using lock(). The mutexes are not swapped.
572 * A swap method accepts a T& and swaps the data inside a critical section.
573 * This is by far the preferred method of changing the guarded datum wholesale because it keeps the lock only
574 * for a short time, thus lowering the pressure on the mutex.
576 void swap(synchronized_value & rhs)
581 // auto _ = make_unique_locks(mtx_, rhs.mtx_);
582 unique_lock<mutex_type> lk1(mtx_, defer_lock);
583 unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
585 boost::swap(value_, rhs.value_);
588 * Swap with the underlying value type
590 * Effects: Swaps the data on a scope protected by the mutex.
592 void swap(value_type & rhs)
594 strict_lock<mutex_type> lk(mtx_);
595 boost::swap(value_, rhs);
599 * Essentially calling a method obj->foo(x, y, z) calls the method foo(x, y, z) inside a critical section as
600 * long-lived as the call itself.
602 strict_lock_ptr<T,Lockable> operator->()
604 return BOOST_THREAD_MAKE_RV_REF((strict_lock_ptr<T,Lockable>(value_, mtx_)));
607 * If the synchronized_value object involved is const-qualified, then you'll only be able to call const methods
608 * through operator->. So, for example, vec->push_back("xyz") won't work if vec were const-qualified.
609 * The locking mechanism capitalizes on the assumption that const methods don't modify their underlying data.
611 const_strict_lock_ptr<T,Lockable> operator->() const
613 return BOOST_THREAD_MAKE_RV_REF((const_strict_lock_ptr<T,Lockable>(value_, mtx_)));
617 * Call function on a locked block.
619 * @requires fct(value_) is well formed.
622 * void fun(synchronized_value<vector<int>> & v) {
623 * v ( [](vector<int>> & vec)
626 * assert(vec.back() == 42);
630 template <typename F>
632 typename boost::result_of<F(value_type&)>::type
633 operator()(BOOST_THREAD_RV_REF(F) fct)
635 strict_lock<mutex_type> lk(mtx_);
638 template <typename F>
640 typename boost::result_of<F(value_type const&)>::type
641 operator()(BOOST_THREAD_RV_REF(F) fct) const
643 strict_lock<mutex_type> lk(mtx_);
648 #if defined BOOST_NO_CXX11_RVALUE_REFERENCES
649 template <typename F>
651 typename boost::result_of<F(value_type&)>::type
652 operator()(F const & fct)
654 strict_lock<mutex_type> lk(mtx_);
657 template <typename F>
659 typename boost::result_of<F(value_type const&)>::type
660 operator()(F const & fct) const
662 strict_lock<mutex_type> lk(mtx_);
666 template <typename R>
668 R operator()(R(*fct)(value_type&))
670 strict_lock<mutex_type> lk(mtx_);
673 template <typename R>
675 R operator()(R(*fct)(value_type const&)) const
677 strict_lock<mutex_type> lk(mtx_);
684 * The synchronize() factory make easier to lock on a scope.
685 * As discussed, operator-> can only lock over the duration of a call, so it is insufficient for complex operations.
686 * With synchronize() you get to lock the object in a scoped and to directly access the object inside that scope.
689 * void fun(synchronized_value<vector<int>> & v) {
690 * auto&& vec=v.synchronize();
692 * assert(vec.back() == 42);
695 strict_lock_ptr<T,Lockable> synchronize()
697 return BOOST_THREAD_MAKE_RV_REF((strict_lock_ptr<T,Lockable>(value_, mtx_)));
699 const_strict_lock_ptr<T,Lockable> synchronize() const
701 return BOOST_THREAD_MAKE_RV_REF((const_strict_lock_ptr<T,Lockable>(value_, mtx_)));
704 unique_lock_ptr<T,Lockable> unique_synchronize()
706 return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_)));
708 const_unique_lock_ptr<T,Lockable> unique_synchronize() const
710 return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_)));
712 unique_lock_ptr<T,Lockable> unique_synchronize(defer_lock_t tag)
714 return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_, tag)));
716 const_unique_lock_ptr<T,Lockable> unique_synchronize(defer_lock_t tag) const
718 return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_, tag)));
720 unique_lock_ptr<T,Lockable> defer_synchronize() BOOST_NOEXCEPT
722 return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_, defer_lock)));
724 const_unique_lock_ptr<T,Lockable> defer_synchronize() const BOOST_NOEXCEPT
726 return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_, defer_lock)));
728 unique_lock_ptr<T,Lockable> try_to_synchronize() BOOST_NOEXCEPT
730 return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_, try_to_lock)));
732 const_unique_lock_ptr<T,Lockable> try_to_synchronize() const BOOST_NOEXCEPT
734 return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_, try_to_lock)));
736 unique_lock_ptr<T,Lockable> adopt_synchronize() BOOST_NOEXCEPT
738 return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_, adopt_lock)));
740 const_unique_lock_ptr<T,Lockable> adopt_synchronize() const BOOST_NOEXCEPT
742 return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_, adopt_lock)));
746 #if ! defined __IBMCPP__
752 friend class synchronized_value;
754 boost::unique_lock<mutex_type> lk_;
757 explicit deref_value(synchronized_value& outer):
758 lk_(outer.mtx_),value_(outer.value_)
762 BOOST_THREAD_MOVABLE_ONLY(deref_value)
764 deref_value(BOOST_THREAD_RV_REF(deref_value) other):
765 lk_(boost::move(BOOST_THREAD_RV(other).lk_)),value_(BOOST_THREAD_RV(other).value_)
772 deref_value& operator=(T const& newVal)
778 class const_deref_value
781 friend class synchronized_value;
783 boost::unique_lock<mutex_type> lk_;
786 explicit const_deref_value(synchronized_value const& outer):
787 lk_(outer.mtx_), value_(outer.value_)
791 BOOST_THREAD_MOVABLE_ONLY(const_deref_value)
793 const_deref_value(BOOST_THREAD_RV_REF(const_deref_value) other):
794 lk_(boost::move(BOOST_THREAD_RV(other).lk_)), value_(BOOST_THREAD_RV(other).value_)
804 deref_value operator*()
806 return BOOST_THREAD_MAKE_RV_REF(deref_value(*this));
809 const_deref_value operator*() const
811 return BOOST_THREAD_MAKE_RV_REF(const_deref_value(*this));
816 * @requires T is OutputStreamable
817 * @effects saves the value type on the output stream @c os.
819 template <typename OStream>
820 void save(OStream& os) const
822 strict_lock<mutex_type> lk(mtx_);
826 * @requires T is InputStreamable
827 * @effects loads the value type from the input stream @c is.
829 template <typename IStream>
830 void load(IStream& is)
832 strict_lock<mutex_type> lk(mtx_);
836 // relational operators
838 * @requires T is EqualityComparable
841 bool operator==(synchronized_value const& rhs) const
843 unique_lock<mutex_type> lk1(mtx_, defer_lock);
844 unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
847 return value_ == rhs.value_;
850 * @requires T is LessThanComparable
853 bool operator<(synchronized_value const& rhs) const
855 unique_lock<mutex_type> lk1(mtx_, defer_lock);
856 unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
859 return value_ < rhs.value_;
862 * @requires T is GreaterThanComparable
865 bool operator>(synchronized_value const& rhs) const
867 unique_lock<mutex_type> lk1(mtx_, defer_lock);
868 unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
871 return value_ > rhs.value_;
873 bool operator<=(synchronized_value const& rhs) const
875 unique_lock<mutex_type> lk1(mtx_, defer_lock);
876 unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
879 return value_ <= rhs.value_;
881 bool operator>=(synchronized_value const& rhs) const
883 unique_lock<mutex_type> lk1(mtx_, defer_lock);
884 unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
887 return value_ >= rhs.value_;
889 bool operator==(value_type const& rhs) const
891 unique_lock<mutex_type> lk1(mtx_);
893 return value_ == rhs;
895 bool operator!=(value_type const& rhs) const
897 unique_lock<mutex_type> lk1(mtx_);
899 return value_ != rhs;
901 bool operator<(value_type const& rhs) const
903 unique_lock<mutex_type> lk1(mtx_);
907 bool operator<=(value_type const& rhs) const
909 unique_lock<mutex_type> lk1(mtx_);
911 return value_ <= rhs;
913 bool operator>(value_type const& rhs) const
915 unique_lock<mutex_type> lk1(mtx_);
919 bool operator>=(value_type const& rhs) const
921 unique_lock<mutex_type> lk1(mtx_);
923 return value_ >= rhs;
928 // Specialized algorithms
932 template <typename T, typename L>
933 inline void swap(synchronized_value<T,L> & lhs, synchronized_value<T,L> & rhs)
937 template <typename T, typename L>
938 inline void swap(synchronized_value<T,L> & lhs, T & rhs)
942 template <typename T, typename L>
943 inline void swap(T & lhs, synchronized_value<T,L> & rhs)
950 // template <class T> struct hash;
951 // template <typename T, typename L>
952 // struct hash<synchronized_value<T,L> >;
955 template <typename T, typename L>
956 bool operator!=(synchronized_value<T,L> const&lhs, synchronized_value<T,L> const& rhs)
961 template <typename T, typename L>
962 bool operator==(T const& lhs, synchronized_value<T,L> const&rhs)
966 template <typename T, typename L>
967 bool operator!=(T const& lhs, synchronized_value<T,L> const&rhs)
971 template <typename T, typename L>
972 bool operator<(T const& lhs, synchronized_value<T,L> const&rhs)
976 template <typename T, typename L>
977 bool operator<=(T const& lhs, synchronized_value<T,L> const&rhs)
981 template <typename T, typename L>
982 bool operator>(T const& lhs, synchronized_value<T,L> const&rhs)
986 template <typename T, typename L>
987 bool operator>=(T const& lhs, synchronized_value<T,L> const&rhs)
995 template <typename OStream, typename T, typename L>
996 inline OStream& operator<<(OStream& os, synchronized_value<T,L> const& rhs)
1001 template <typename IStream, typename T, typename L>
1002 inline IStream& operator>>(IStream& is, synchronized_value<T,L>& rhs)
1008 #if ! defined(BOOST_THREAD_NO_SYNCHRONIZE)
1009 #if ! defined BOOST_NO_CXX11_VARIADIC_TEMPLATES
1011 template <typename ...SV>
1012 std::tuple<typename synchronized_value_strict_lock_ptr<SV>::type ...> synchronize(SV& ...sv)
1014 boost::lock(sv.mtx_ ...);
1015 typedef std::tuple<typename synchronized_value_strict_lock_ptr<SV>::type ...> t_type;
1017 return t_type(typename synchronized_value_strict_lock_ptr<SV>::type(sv.value_, sv.mtx_, adopt_lock) ...);
1021 template <typename SV1, typename SV2>
1023 typename synchronized_value_strict_lock_ptr<SV1>::type,
1024 typename synchronized_value_strict_lock_ptr<SV2>::type
1026 synchronize(SV1& sv1, SV2& sv2)
1028 boost::lock(sv1.mtx_, sv2.mtx_);
1030 typename synchronized_value_strict_lock_ptr<SV1>::type,
1031 typename synchronized_value_strict_lock_ptr<SV2>::type
1035 typename synchronized_value_strict_lock_ptr<SV1>::type(sv1.value_, sv1.mtx_, adopt_lock),
1036 typename synchronized_value_strict_lock_ptr<SV2>::type(sv2.value_, sv2.mtx_, adopt_lock)
1040 template <typename SV1, typename SV2, typename SV3>
1042 typename synchronized_value_strict_lock_ptr<SV1>::type,
1043 typename synchronized_value_strict_lock_ptr<SV2>::type,
1044 typename synchronized_value_strict_lock_ptr<SV3>::type
1046 synchronize(SV1& sv1, SV2& sv2, SV3& sv3)
1048 boost::lock(sv1.mtx_, sv2.mtx_);
1050 typename synchronized_value_strict_lock_ptr<SV1>::type,
1051 typename synchronized_value_strict_lock_ptr<SV2>::type,
1052 typename synchronized_value_strict_lock_ptr<SV3>::type
1056 typename synchronized_value_strict_lock_ptr<SV1>::type(sv1.value_, sv1.mtx_, adopt_lock),
1057 typename synchronized_value_strict_lock_ptr<SV2>::type(sv2.value_, sv2.mtx_, adopt_lock),
1058 typename synchronized_value_strict_lock_ptr<SV3>::type(sv3.value_, sv3.mtx_, adopt_lock)
1066 #include <boost/config/abi_suffix.hpp>