Boost logo

Boost-Commit :

From: anthony_at_[hidden]
Date: 2008-08-19 06:26:53


Author: anthonyw
Date: 2008-08-19 06:26:53 EDT (Tue, 19 Aug 2008)
New Revision: 48213
URL: http://svn.boost.org/trac/boost/changeset/48213

Log:
Added missing relative time constructor to unique_lock
Text files modified:
   trunk/boost/thread/locks.hpp | 6 +
   trunk/libs/thread/test/test_mutex.cpp | 140 ++++++++++++++++++++++-----------------
   2 files changed, 86 insertions(+), 60 deletions(-)

Modified: trunk/boost/thread/locks.hpp
==============================================================================
--- trunk/boost/thread/locks.hpp (original)
+++ trunk/boost/thread/locks.hpp 2008-08-19 06:26:53 EDT (Tue, 19 Aug 2008)
@@ -234,6 +234,12 @@
         {
             try_lock();
         }
+ template<typename TimeDuration>
+ unique_lock(Mutex& m_,TimeDuration const& target_time):
+ m(&m_),is_locked(false)
+ {
+ timed_lock(target_time);
+ }
         unique_lock(Mutex& m_,system_time const& target_time):
             m(&m_),is_locked(false)
         {

Modified: trunk/libs/thread/test/test_mutex.cpp
==============================================================================
--- trunk/libs/thread/test/test_mutex.cpp (original)
+++ trunk/libs/thread/test/test_mutex.cpp 2008-08-19 06:26:53 EDT (Tue, 19 Aug 2008)
@@ -97,6 +97,86 @@
     }
 };
 
+template<typename Mutex>
+struct test_lock_times_out_if_other_thread_has_lock
+{
+ typedef boost::unique_lock<Mutex> Lock;
+
+ Mutex m;
+ boost::mutex done_mutex;
+ bool done;
+ bool locked;
+ boost::condition_variable done_cond;
+
+ test_lock_times_out_if_other_thread_has_lock():
+ done(false),locked(false)
+ {}
+
+ void locking_thread()
+ {
+ Lock lock(m,boost::defer_lock);
+ lock.timed_lock(boost::posix_time::milliseconds(50));
+
+ boost::lock_guard<boost::mutex> lk(done_mutex);
+ locked=lock.owns_lock();
+ done=true;
+ done_cond.notify_one();
+ }
+
+ void locking_thread_through_constructor()
+ {
+ Lock lock(m,boost::posix_time::milliseconds(50));
+
+ boost::lock_guard<boost::mutex> lk(done_mutex);
+ locked=lock.owns_lock();
+ done=true;
+ done_cond.notify_one();
+ }
+
+ bool is_done() const
+ {
+ return done;
+ }
+
+ typedef test_lock_times_out_if_other_thread_has_lock<Mutex> this_type;
+
+ void do_test(void (this_type::*test_func)())
+ {
+ Lock lock(m);
+
+ locked=false;
+ done=false;
+
+ boost::thread t(test_func,this);
+
+ try
+ {
+ {
+ boost::mutex::scoped_lock lk(done_mutex);
+ BOOST_CHECK(done_cond.timed_wait(lk,boost::posix_time::seconds(2),
+ boost::bind(&this_type::is_done,this)));
+ BOOST_CHECK(!locked);
+ }
+
+ lock.unlock();
+ t.join();
+ }
+ catch(...)
+ {
+ lock.unlock();
+ t.join();
+ throw;
+ }
+ }
+
+
+ void operator()()
+ {
+ do_test(&this_type::locking_thread);
+ do_test(&this_type::locking_thread_through_constructor);
+ }
+};
+
 template <typename M>
 struct test_timedlock
 {
@@ -181,66 +261,6 @@
     }
 };
 
-template<typename Mutex>
-struct test_lock_times_out_if_other_thread_has_lock
-{
- typedef boost::unique_lock<Mutex> Lock;
-
- Mutex m;
- boost::mutex done_mutex;
- bool done;
- bool locked;
- boost::condition_variable done_cond;
-
- test_lock_times_out_if_other_thread_has_lock():
- done(false),locked(false)
- {}
-
- void locking_thread()
- {
- Lock lock(m,boost::defer_lock);
- lock.timed_lock(boost::posix_time::milliseconds(50));
-
- boost::lock_guard<boost::mutex> lk(done_mutex);
- locked=lock.owns_lock();
- done=true;
- done_cond.notify_one();
- }
-
- bool is_done() const
- {
- return done;
- }
-
-
- void operator()()
- {
- Lock lock(m);
-
- typedef test_lock_times_out_if_other_thread_has_lock<Mutex> this_type;
-
- boost::thread t(&this_type::locking_thread,this);
-
- try
- {
- {
- boost::mutex::scoped_lock lk(done_mutex);
- BOOST_CHECK(done_cond.timed_wait(lk,boost::posix_time::seconds(2),
- boost::bind(&this_type::is_done,this)));
- BOOST_CHECK(!locked);
- }
-
- lock.unlock();
- t.join();
- }
- catch(...)
- {
- lock.unlock();
- t.join();
- throw;
- }
- }
-};
 
 void do_test_mutex()
 {


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