Opened 9 years ago

Closed 8 years ago

#9720 closed Bugs (fixed)

boost::this_thread::sleep_for() sometimes returns immediately on win32

Reported by: Lars Hagström <lars@…> Owned by: viboes
Milestone: Component: thread
Version: Boost 1.55.0 Severity: Problem
Keywords: chrono thread QueryPerformanceCounter Cc:

Description

I have observed one instance of boost::this_thread::sleep_for returning immediately instead of waiting for the specified time. This happened on a Windows 7 32 bit machine, with Boost 1.55 and VS2012. Another relevant thing might be that it is a VM with 2 cpus running in XenServer 6.2.

Attached is my test program, that starts a bunch of threads that all do a sleep_for(10 minutes). Main then immediately interrupts and joins the threads. The threads all check whether they timed out or whether they were interrupted. In case they time out we print the elapsed time, which of course is expected to be 10 minutes.

Here is the output of my test program:

first test
Timeout!
Elapsed time:  0.000039s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)
.

I'm thinking that this could be something to do with the well known strangeness of QueryPerformanceCounter (see for example #8006). The VM host was probably under a fair bit of load at the time this test failed.

Attachments (2)

BoostSleep_test.cpp (2.4 KB ) - added by Lars Hagström <lars@…> 9 years ago.
Test of this_thread::sleep_for and interruption.
test_9720.cpp (2.5 KB ) - added by viboes 9 years ago.
Could you try this one using the develop branch?

Download all attachments as: .zip

Change History (18)

by Lars Hagström <lars@…>, 9 years ago

Attachment: BoostSleep_test.cpp added

Test of this_thread::sleep_for and interruption.

comment:1 by viboes, 9 years ago

Owner: changed from Anthony Williams to viboes
Status: newassigned

comment:2 by viboes, 9 years ago

Please could you rework the example without using boost::timer and using a steady_clock or high_resolution_clock?

Last edited 9 years ago by viboes (previous) (diff)

by viboes, 9 years ago

Attachment: test_9720.cpp added

Could you try this one using the develop branch?

comment:3 by Lars Hagström <lars@…>, 9 years ago

I've now got this happening regularly. It appears to only happen at the very start of running the program. So I've rewritten it with just chrono and no timers, and also added a python script that runs it repeatedly.

Can be found here (an svn repo): http://svn.code.sf.net/p/safir/svn/misc/boost_sleep_test/

Anyway, this now fails quite quickly on a VM like the one I described above.

Timeout!
Elapsed time: 601116317640 nanoseconds.

Will try with the develop branch "soon" (am home with sick kid atm).

comment:4 by Lars Hagström <lars@…>, 9 years ago

I just realized what the problem is!

It's not a QPC problem after all. It's the static in steady_clock::now. I've got 50 threads trying to initialize that static variable at the same time, of course things go wrong :-)

No idea of how to fix it in an efficient manner, though.

in reply to:  4 comment:5 by viboes, 9 years ago

Replying to Lars Hagström <lars@…>:

I just realized what the problem is!

It's not a QPC problem after all. It's the static in steady_clock::now. I've got 50 threads trying to initialize that static variable at the same time, of course things go wrong :-)

No idea of how to fix it in an efficient manner, though.

Do you mean the static in

    static double nanosecs_per_tic = chrono_detail::get_nanosecs_per_tic();

Could you try removing the static keyword?

  steady_clock::time_point steady_clock::now() BOOST_NOEXCEPT
  {
    double nanosecs_per_tic = chrono_detail::get_nanosecs_per_tic();

comment:6 by Lars Hagström <lars@…>, 9 years ago

Yes, that is exactly the one I mean.

I've removed the static keyword and it works very well now.

comment:7 by viboes, 9 years ago

Component: threadchrono
Milestone: To Be DeterminedBoost 1.56.0

comment:8 by viboes, 9 years ago

Thanks, I have changed to Chrono. I will propose you a complete patch soon.

comment:10 by Lars Hagström <lars@…>, 9 years ago

That certainly works. Thanks!

comment:12 by Smithe329, 8 years ago

Component: chronoxpressive
Milestone: Boost 1.56.0Website 1.X
Resolution: fixed
Severity: ProblemNot Applicable
Status: closedreopened
Type: BugsLibrary Submissions
Version: Boost 1.55.0Boost.Build-M3

If you are going for best contents like myself, simply go to see this site all ekebkcegekkdbdde

comment:13 by anonymous, 8 years ago

When are you guys going to fix it?

comment:14 by viboes, 8 years ago

Component: xpressivethread
Milestone: Website 1.XTo Be Determined
Severity: Not ApplicableProblem
Type: Library SubmissionsBugs
Version: Boost.Build-M3Boost 1.55.0

Smithe329, we can be wrong when we do some changes on the Trac. This doesn't mean that you don't need to be polite.

comment:15 by viboes, 8 years ago

It seems this is related to #9856 and #10967.

comment:16 by viboes, 8 years ago

Milestone: To Be Determined
Resolution: fixed
Status: reopenedclosed

anonymous please, if this is not working for you give more information. This was fixed as Lars said "That certainly works. Thanks!".

Note: See TracTickets for help on using tickets.