[Boost-bugs] [Boost C++ Libraries] #5034: boost::this_thread::sleep either returns instantaneously or spins

Subject: [Boost-bugs] [Boost C++ Libraries] #5034: boost::this_thread::sleep either returns instantaneously or spins
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-01-02 21:39:01


#5034: boost::this_thread::sleep either returns instantaneously or spins
------------------------------+---------------------------------------------
 Reporter: Jamie Allsop | Owner: anthonyw
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: thread
  Version: Boost 1.45.0 | Severity: Showstopper
 Keywords: |
------------------------------+---------------------------------------------
 From the docs I'd expect to be able to write this:

 {{{
 #!cpp
 std::cout
     << "Time Before Sleep: "
     << boost::posix_time::microsec_clock::local_time()
     << std::endl;

 boost::this_thread::sleep( boost::posix_time::seconds(10) );

 std::cout
     << "Time After Sleep : "
     << boost::posix_time::microsec_clock::local_time()
     << std::endl;
 }}}

 and have the 'this' thread sleep for approximately 10 seconds. Instead
 sleep returns instantaneously:

 {{{
 Time Before Sleep: 2010-Dec-25 01:38:50.949160000
 Time After Sleep : 2010-Dec-25 01:38:50.949436000
 }}}

 I've tried this with boost 1.45 and boost 1.43 using gcc 4.4 on debian
 x86_64. This used to work for me on 32-bit debian unstable using gcc 4.3.

 In addition to sleep returning instantaneously (happens every time) I have
 observed with more complex scenarios involving multiple threads sleep
 enter a busy spin, never returning (process needs killed). I am trying to
 create a minimal test case for this.

 Rather obviously the code where this hits is here in
 source:/boost/tags/release/Boost_1_45_0/libs/thread/src/pthread/thread.cpp

 {{{
 #!cpp
         void sleep(const system_time& st)
         {
             detail::thread_data_base* const
 thread_info=detail::get_current_thread_data();

             if(thread_info)
             {
                 unique_lock<mutex> lk(thread_info->sleep_mutex);
                 while(thread_info->sleep_condition.timed_wait(lk,st));
             }
             else
             {
                 xtime const xt=get_xtime(st);

                 for (int foo=0; foo < 5; ++foo)
                 {
 # if defined(BOOST_HAS_PTHREAD_DELAY_NP)
                     timespec ts;
                     to_timespec_duration(xt, ts);
                     BOOST_VERIFY(!pthread_delay_np(&ts));
 # elif defined(BOOST_HAS_NANOSLEEP)
                     timespec ts;
                     to_timespec_duration(xt, ts);

                     // nanosleep takes a timespec that is an offset, not
                     // an absolute time.
                     nanosleep(&ts, 0);
 # else
                     mutex mx;
                     mutex::scoped_lock lock(mx);
                     condition cond;
                     cond.timed_wait(lock, xt);
 # endif
                     xtime cur;
                     xtime_get(&cur, TIME_UTC);
                     if (xtime_cmp(xt, cur) <= 0)
                         return;
                 }
             }
         }

 }}}

 Specifically something appears to go wrong here:

 {{{
             if(thread_info)
             {
                 unique_lock<mutex> lk(thread_info->sleep_mutex);
                 while(thread_info->sleep_condition.timed_wait(lk,st));
             }
 }}}

 I came across this while writing a test for something else and so have not
 been able to follow up in any more detail yet at this time.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/5034>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:05 UTC