Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r83086 - in trunk: boost/thread libs/thread/example
From: vicente.botet_at_[hidden]
Date: 2013-02-22 12:42:45


Author: viboes
Date: 2013-02-22 12:42:44 EST (Fri, 22 Feb 2013)
New Revision: 83086
URL: http://svn.boost.org/trac/boost/changeset/83086

Log:
Thread: Added value semantics to synchronized value
Text files modified:
   trunk/boost/thread/synchronized_value.hpp | 155 +++++++++++++++++++++++++++++++++++++++
   trunk/libs/thread/example/synchronized_value.cpp | 78 ++++++++++++++++++--
   2 files changed, 224 insertions(+), 9 deletions(-)

Modified: trunk/boost/thread/synchronized_value.hpp
==============================================================================
--- trunk/boost/thread/synchronized_value.hpp (original)
+++ trunk/boost/thread/synchronized_value.hpp 2013-02-22 12:42:44 EST (Fri, 22 Feb 2013)
@@ -225,6 +225,7 @@
     T value_;
     mutable lockable_type mtx_;
   public:
+ // construction/destruction
     /**
      * Default constructor.
      *
@@ -277,6 +278,7 @@
       value_= boost::move(other.value);
     }
 
+ // mutation
     /**
      * Assignment operator.
      *
@@ -314,6 +316,7 @@
       return *this;
     }
 
+ //observers
     /**
      * Explicit conversion to value type.
      *
@@ -339,7 +342,6 @@
       return get();
     }
 #endif
-
     /**
      * Swap
      *
@@ -497,8 +499,99 @@
       return BOOST_THREAD_MAKE_RV_REF(const_deref_value(*this));
     }
 
+ template <typename OStream>
+ void save(OStream& os) const
+ {
+ strict_lock<lockable_type> lk(mtx_);
+ os << value_;
+ }
+ template <typename IStream>
+ void load(IStream& is) const
+ {
+ strict_lock<lockable_type> lk(mtx_);
+ is >> value_;
+ }
+
+ bool operator==(synchronized_value const& rhs) const
+ {
+ unique_lock<lockable_type> lk1(mtx_, defer_lock);
+ unique_lock<lockable_type> lk2(rhs.mtx_, defer_lock);
+ lock(lk1,lk2);
+
+ return value_ == rhs.value_;
+ }
+ bool operator<(synchronized_value const& rhs) const
+ {
+ unique_lock<lockable_type> lk1(mtx_, defer_lock);
+ unique_lock<lockable_type> lk2(rhs.mtx_, defer_lock);
+ lock(lk1,lk2);
+
+ return value_ < rhs.value_;
+ }
+ bool operator>(synchronized_value const& rhs) const
+ {
+ unique_lock<lockable_type> lk1(mtx_, defer_lock);
+ unique_lock<lockable_type> lk2(rhs.mtx_, defer_lock);
+ lock(lk1,lk2);
+
+ return value_ > rhs.value_;
+ }
+ bool operator<=(synchronized_value const& rhs) const
+ {
+ unique_lock<lockable_type> lk1(mtx_, defer_lock);
+ unique_lock<lockable_type> lk2(rhs.mtx_, defer_lock);
+ lock(lk1,lk2);
+
+ return value_ <= rhs.value_;
+ }
+ bool operator>=(synchronized_value const& rhs) const
+ {
+ unique_lock<lockable_type> lk1(mtx_, defer_lock);
+ unique_lock<lockable_type> lk2(rhs.mtx_, defer_lock);
+ lock(lk1,lk2);
+
+ return value_ >= rhs.value_;
+ }
+ bool operator==(value_type const& rhs) const
+ {
+ unique_lock<lockable_type> lk1(mtx_);
+
+ return value_ == rhs;
+ }
+ bool operator!=(value_type const& rhs) const
+ {
+ unique_lock<lockable_type> lk1(mtx_);
+
+ return value_ != rhs;
+ }
+ bool operator<(value_type const& rhs) const
+ {
+ unique_lock<lockable_type> lk1(mtx_);
+
+ return value_ < rhs;
+ }
+ bool operator<=(value_type const& rhs) const
+ {
+ unique_lock<lockable_type> lk1(mtx_);
+
+ return value_ <= rhs;
+ }
+ bool operator>(value_type const& rhs) const
+ {
+ unique_lock<lockable_type> lk1(mtx_);
+
+ return value_ > rhs;
+ }
+ bool operator>=(value_type const& rhs) const
+ {
+ unique_lock<lockable_type> lk1(mtx_);
+
+ return value_ >= rhs;
+ }
+
   };
 
+ // Specialized algorithms
   /**
    *
    */
@@ -508,6 +601,66 @@
     lhs.swap(rhs);
   }
 
+ //Hash support
+
+ template <class T> struct hash;
+ template <typename T, typename L>
+ struct hash<synchronized_value<T,L> >;
+
+ // Comparison with T
+ template <typename T, typename L>
+ bool operator!=(synchronized_value<T,L> const&lhs, synchronized_value<T,L> const& rhs)
+ {
+ return ! (lhs==rhs);
+ }
+
+ template <typename T, typename L>
+ bool operator==(T const& lhs, synchronized_value<T,L> const&rhs)
+ {
+ return rhs==lhs;
+ }
+ template <typename T, typename L>
+ bool operator!=(T const& lhs, synchronized_value<T,L> const&rhs)
+ {
+ return rhs!=lhs;
+ }
+ template <typename T, typename L>
+ bool operator<(T const& lhs, synchronized_value<T,L> const&rhs)
+ {
+ return rhs>=lhs;
+ }
+ template <typename T, typename L>
+ bool operator<=(T const& lhs, synchronized_value<T,L> const&rhs)
+ {
+ return rhs>lhs;
+ }
+ template <typename T, typename L>
+ bool operator>(T const& lhs, synchronized_value<T,L> const&rhs)
+ {
+ return rhs<=lhs;
+ }
+ template <typename T, typename L>
+ bool operator>=(T const& lhs, synchronized_value<T,L> const&rhs)
+ {
+ return rhs<lhs;
+ }
+
+ /**
+ *
+ */
+ template <typename OStream, typename T, typename L>
+ inline OStream& operator<<(OStream& os, synchronized_value<T,L> const& rhs)
+ {
+ rhs.save(os);
+ return os;
+ }
+ template <typename IStream, typename T, typename L>
+ inline IStream& operator>>(IStream& is, synchronized_value<T,L> const& rhs)
+ {
+ rhs.load(is);
+ return is;
+ }
+
 }
 
 #include <boost/config/abi_suffix.hpp>

Modified: trunk/libs/thread/example/synchronized_value.cpp
==============================================================================
--- trunk/libs/thread/example/synchronized_value.cpp (original)
+++ trunk/libs/thread/example/synchronized_value.cpp 2013-02-22 12:42:44 EST (Fri, 22 Feb 2013)
@@ -10,15 +10,15 @@
 #include <string>
 #include <boost/thread/synchronized_value.hpp>
 
-void addTrailingSlashIfMissing(boost::synchronized_value<std::string> & path)
-{
- boost::strict_lock_ptr<std::string> u=path.synchronize();
-
- if(u->empty() || (*u->rbegin()!='/'))
+ void addTrailingSlashIfMissing(boost::synchronized_value<std::string> & path)
   {
- *u+='/';
+ boost::strict_lock_ptr<std::string> u=path.synchronize();
+
+ if(u->empty() || (*u->rbegin()!='/'))
+ {
+ *u+='/';
+ }
   }
-}
 
 void f(const boost::synchronized_value<int> &v) {
   std::cout<<"v="<<*v<<std::endl;
@@ -80,6 +80,68 @@
     addTrailingSlashIfMissing(s);
     std::cout<<"s="<<std::string(*s)<<std::endl;
   }
- return 0;
+ {
+ boost::synchronized_value<std::string> s;
+ s = std::string("foo/");
+ std::cout<<"ss="<< s << std::endl;
+ }
+ {
+ boost::synchronized_value<std::string> s;
+ s = "foo/";
+ std::cout<<"ss="<< s << std::endl;
+ }
+ {
+ boost::synchronized_value<std::string> s1("a");
+ boost::synchronized_value<std::string> s2;
+ s2=s1;
+ std::cout<<"s1="<< s1 << std::endl;
+ std::cout<<"s2="<< s2 << std::endl;
+ }
+ {
+ boost::synchronized_value<std::string> s1("a");
+ boost::synchronized_value<std::string> s2("b");
+ std::cout<<"s1="<< s1 << std::endl;
+ std::cout<<"s2="<< s2 << std::endl;
+ swap(s1,s2);
+ std::cout<<"s1="<< s1 << std::endl;
+ std::cout<<"s2="<< s2 << std::endl;
+ }
+#if ! defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
+ {
+ boost::synchronized_value<std::string> sts("a");
+ std::string s(sts);
+ std::cout<<"ssts="<< s << std::endl;
+ }
+#endif
+ {
+ boost::synchronized_value<int> s1(1);
+ boost::synchronized_value<int> s2(1);
+ BOOST_ASSERT(s1==s2);
+ BOOST_ASSERT(s1<=s2);
+ BOOST_ASSERT(s1>=s2);
+ BOOST_ASSERT(s1==1);
+ BOOST_ASSERT(s1<=1);
+ BOOST_ASSERT(s1>=1);
+ }
+ {
+ boost::synchronized_value<int> s1(1);
+ boost::synchronized_value<int> s2(2);
+ BOOST_ASSERT(s1!=s2);
+ BOOST_ASSERT(s1!=2);
+ BOOST_ASSERT(2!=s1);
+ }
+ {
+ boost::synchronized_value<int> s1(1);
+ boost::synchronized_value<int> s2(2);
+ BOOST_ASSERT(s1<s2);
+ BOOST_ASSERT(s1<=s2);
+ BOOST_ASSERT(s2>s1);
+ BOOST_ASSERT(s2>=s1);
+ BOOST_ASSERT(s1<2);
+ BOOST_ASSERT(s1<=2);
+ BOOST_ASSERT(s2>1);
+ BOOST_ASSERT(s2>=1);
+ }
+ return 1;
 }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk