Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r84056 - in trunk: boost/thread libs/thread/test
From: vicente.botet_at_[hidden]
Date: 2013-04-27 10:39:46


Author: viboes
Date: 2013-04-27 10:39:45 EDT (Sat, 27 Apr 2013)
New Revision: 84056
URL: http://svn.boost.org/trac/boost/changeset/84056

Log:
Thread: try to fix time based functions.
Text files modified:
   trunk/boost/thread/completion_latch.hpp | 50 ++++++++++++++++++++---------------
   trunk/boost/thread/latch.hpp | 56 +++++++++++++++++++++++----------------
   trunk/libs/thread/test/test_completion_latch.cpp | 20 ++-----------
   trunk/libs/thread/test/test_latch.cpp | 4 ++
   4 files changed, 68 insertions(+), 62 deletions(-)

Modified: trunk/boost/thread/completion_latch.hpp
==============================================================================
--- trunk/boost/thread/completion_latch.hpp (original)
+++ trunk/boost/thread/completion_latch.hpp 2013-04-27 10:39:45 EDT (Sat, 27 Apr 2013)
@@ -7,6 +7,7 @@
 #define BOOST_THREAD_COMPLETION_LATCH_HPP
 
 #include <boost/thread/detail/config.hpp>
+#include <boost/thread/detail/delete.hpp>
 
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/lock_types.hpp>
@@ -43,7 +44,7 @@
     /// noop completion function factory
     static completion_function noop()
     {
- return completion_function (&thread_detail::noop);
+ return completion_function(&thread_detail::noop);
     }
 
   private:
@@ -158,6 +159,11 @@
     {
     }
 
+ ///
+ ~completion_latch()
+ {
+
+ }
     /// Blocks until the latch has counted down to zero.
     void wait()
     {
@@ -183,34 +189,34 @@
       boost::unique_lock<boost::mutex> lk(mutex_);
       pre_wait(lk);
       cv_status res;
- if (count_ > 0)
+ while(count_ > 0)
       {
- res = count_cond_.wait_for(rel_time);
- }
- else
- {
- res = cv_status::no_timeout;
+ if (count_cond_.wait_for(lk,rel_time)==cv_status::timeout)
+ {
+ res = (count_ == 0 ? cv_status::no_timeout : cv_status::timeout);
+ }
       }
+ res = cv_status::no_timeout;
       post_wait(lk);
       return res;
     }
 
     /// try to wait until the specified time_point is reached
     /// @return whether there is a timeout or not.
- template <class lock_type, class Clock, class Duration>
+ template <class Clock, class Duration>
     cv_status wait_until(const chrono::time_point<Clock, Duration>& abs_time)
     {
       boost::unique_lock<boost::mutex> lk(mutex_);
       pre_wait(lk);
       cv_status res;
- if (count_ > 0)
- {
- res = count_cond_.wait_until(abs_time);
- }
- else
+ while(count_ > 0)
       {
- res = cv_status::no_timeout;
+ if (count_cond_.wait_until(lk,abs_time)==cv_status::timeout)
+ {
+ res = (count_ == 0 ? cv_status::no_timeout : cv_status::timeout);
+ }
       }
+ res = cv_status::no_timeout;
       post_wait(lk);
       return res;
     }
@@ -261,14 +267,14 @@
     /// @Returns the old completion function if any or noop if
 
 #ifdef BOOST_NO_CXX11_HDR_FUNCTIONAL
- template <typename F>
- completion_function then(BOOST_THREAD_RV_REF(F) funct)
- {
- boost::lock_guard<boost::mutex> lk(mutex_);
- completion_function tmp(funct_);
- funct_ = boost::move(funct);
- return tmp;
- }
+ template <typename F>
+ completion_function then(BOOST_THREAD_RV_REF(F) funct)
+ {
+ boost::lock_guard<boost::mutex> lk(mutex_);
+ completion_function tmp(funct_);
+ funct_ = boost::move(funct);
+ return tmp;
+ }
 #endif
     completion_function then(void(*funct)())
     {

Modified: trunk/boost/thread/latch.hpp
==============================================================================
--- trunk/boost/thread/latch.hpp (original)
+++ trunk/boost/thread/latch.hpp 2013-04-27 10:39:45 EDT (Sat, 27 Apr 2013)
@@ -7,6 +7,7 @@
 #define BOOST_THREAD_LATCH_HPP
 
 #include <boost/thread/detail/config.hpp>
+#include <boost/thread/detail/delete.hpp>
 
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/lock_types.hpp>
@@ -15,10 +16,6 @@
 #include <boost/chrono/time_point.hpp>
 #include <boost/assert.hpp>
 
-//#include <boost/throw_exception.hpp>
-//#include <stdexcept>
-//#include <string>
-
 #include <boost/config/abi_prefix.hpp>
 
 namespace boost
@@ -30,7 +27,7 @@
     {
       while (count_ > 0)
       {
- cond_.wait(lk);
+ count_cond_.wait(lk);
       }
     }
     /// Decrement the count and notify anyone waiting if we reach zero.
@@ -41,7 +38,7 @@
       BOOST_ASSERT(count_ > 0);
       if (--count_ == 0)
       {
- cond_.notify_all();
+ count_cond_.notify_all();
         lk.unlock();
         return true;
       }
@@ -49,7 +46,7 @@
     }
 
   public:
- BOOST_THREAD_NO_COPYABLE( latch )
+ BOOST_THREAD_NO_COPYABLE( latch)
 
     /// Constructs a latch with a given count.
     latch(std::size_t count) :
@@ -57,6 +54,12 @@
     {
     }
 
+ ///
+ ~latch()
+ {
+
+ }
+
     /// Blocks until the latch has counted down to zero.
     void wait()
     {
@@ -77,30 +80,31 @@
     cv_status wait_for(const chrono::duration<Rep, Period>& rel_time)
     {
       boost::unique_lock<boost::mutex> lk(mutex_);
- if (count_ > 0)
- {
- return cond_.wait_for(rel_time);
- }
- else
+ while(count_ > 0)
       {
- return cv_status::no_timeout;
+ if (count_cond_.wait_for(lk,rel_time)==cv_status::timeout)
+ {
+ return (count_ == 0 ? cv_status::no_timeout : cv_status::timeout);
+ }
       }
+ return cv_status::no_timeout;
+
     }
 
     /// try to wait until the specified time_point is reached
     /// @return whether there is a timeout or not.
- template <class lock_type, class Clock, class Duration>
+ template <class Clock, class Duration>
     cv_status wait_until(const chrono::time_point<Clock, Duration>& abs_time)
     {
       boost::unique_lock<boost::mutex> lk(mutex_);
- if (count_ > 0)
- {
- return cond_.wait_until(abs_time);
- }
- else
+ while(count_ > 0)
       {
- return cv_status::no_timeout;
+ if (count_cond_.wait_until(lk,abs_time)==cv_status::timeout)
+ {
+ return (count_ == 0 ? cv_status::no_timeout : cv_status::timeout);
+ }
       }
+ return cv_status::no_timeout;
     }
 
     /// Decrement the count and notify anyone waiting if we reach zero.
@@ -110,7 +114,10 @@
       boost::unique_lock<boost::mutex> lk(mutex_);
       count_down(lk);
     }
- void signal() {count_down();}
+ void signal()
+ {
+ count_down();
+ }
 
     /// Decrement the count and notify anyone waiting if we reach zero.
     /// Blocks until the latch has counted down to zero.
@@ -124,7 +131,10 @@
       }
       wait(lk);
     }
- void sync() {count_down_and_wait();}
+ void sync()
+ {
+ count_down_and_wait();
+ }
 
     /// Reset the counter
     /// #Requires This method may only be invoked when there are no other threads currently inside the count_down_and_wait() method.
@@ -137,7 +147,7 @@
 
   private:
     mutex mutex_;
- condition_variable cond_;
+ condition_variable count_cond_;
     std::size_t count_;
   };
 

Modified: trunk/libs/thread/test/test_completion_latch.cpp
==============================================================================
--- trunk/libs/thread/test/test_completion_latch.cpp (original)
+++ trunk/libs/thread/test/test_completion_latch.cpp 2013-04-27 10:39:45 EDT (Sat, 27 Apr 2013)
@@ -64,8 +64,9 @@
         g.create_thread(&latch_thread);
 
       if (!gen_latch.try_wait())
- gen_latch.wait(); // All the threads have been updated the global_parameter
- //std::cout << __FILE__ << ':' << __LINE__ << std::endl;
+ if (gen_latch.wait_for(boost::chrono::milliseconds(100)) == boost::cv_status::timeout)
+ if (gen_latch.wait_until(boost::chrono::steady_clock::now()+boost::chrono::milliseconds(100)) == boost::cv_status::timeout)
+ gen_latch.wait(); // All the threads have been updated the global_parameter
       g.join_all();
     }
     catch (...)
@@ -75,21 +76,16 @@
       throw;
     }
   }
- //std::cout << __FILE__ << ':' << __LINE__ << std::endl;
   gen_latch.then(&test_global_parameter);
   {
- //std::cout << __FILE__ << ':' << __LINE__ << std::endl;
     global_parameter = 0;
     try
     {
- //std::cout << __FILE__ << ':' << __LINE__ << std::endl;
       for (int i = 0; i < N_THREADS; ++i)
         g2.create_thread(&latch_thread);
 
- //std::cout << __FILE__ << ':' << __LINE__ << std::endl;
- //if (!gen_latch.try_wait())
+ if (!gen_latch.try_wait())
         gen_latch.wait(); // All the threads have been updated the global_parameter
- //std::cout << __FILE__ << ':' << __LINE__ << std::endl;
 
       g2.join_all();
     }
@@ -101,18 +97,10 @@
     }
   }
 }
-//template <bool b>
-//struct xx {
-// static BOOST_CONSTEXPR_OR_CONST bool value = !b;
-//};
 
 int main()
 {
- //test_completion_latch();
   test_completion_latch_reset();
   return boost::report_errors();
-
- //BOOST_CONSTEXPR_OR_CONST bool a = boost::integral_constant<bool,false>::value;
- //xx<a>
 }
 

Modified: trunk/libs/thread/test/test_latch.cpp
==============================================================================
--- trunk/libs/thread/test/test_latch.cpp (original)
+++ trunk/libs/thread/test/test_latch.cpp 2013-04-27 10:39:45 EDT (Sat, 27 Apr 2013)
@@ -45,7 +45,9 @@
       g.create_thread(&latch_thread);
 
     if (! gen_latch.try_wait())
- gen_latch.wait(); // All the threads have been updated the global_parameter
+ if (gen_latch.wait_for(boost::chrono::milliseconds(100)) == boost::cv_status::timeout)
+ if (gen_latch.wait_until(boost::chrono::steady_clock::now()+boost::chrono::milliseconds(100)) == boost::cv_status::timeout)
+ gen_latch.wait(); // All the threads have been updated the global_parameter
     BOOST_TEST_EQ(global_parameter, N_THREADS);
 
     g.join_all();


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