Opened 8 years ago

Closed 8 years ago

#3178 closed Bugs (fixed)

Sleep with negative time

Reported by: - Owned by: Anthony Williams
Milestone: Boost 1.40.0 Component: thread
Version: Boost 1.39.0 Severity: Regression
Keywords: thread sleep timespan negative Cc:


When trying to sleep with a negative timespan it will result in almost infinite sleep.

Solution: in this_thread sleep, check for negative timespan before calling interruptible_wait.

Attachments (0)

Change History (4)

comment:1 Changed 8 years ago by viboes

As you state this is a regression, could you state on which version this was OK?

comment:2 Changed 8 years ago by Marshall Clow

I've got to concur with the OP (from libs/thread/win32/thread_data.hpp):

    namespace this_thread {
        template<typename TimeDuration>
        inline void sleep(TimeDuration const& rel_time)
            interruptible_wait(static_cast<unsigned long>(rel_time.total_milliseconds()));

The problem here is the cast to unsigned long; that turns a small negative value into a large positive one. The question is, what should happen? Should the fn return immediately? Throw an exception?

On the pthread side, the code looks like:

    namespace this_thread {
        inline void sleep(TimeDuration const& rel_time)

I don't know what happens if you pass a time in the past to sleep here, but the same questions as to how to handle the error apply.

comment:3 Changed 8 years ago by viboes

I don't think the low level functions must check for these on the past wait, for me this should be undefined behavior (the documentation should state this for all the timeout based operation). The user can always define its own function that do the check. If people consider that this is useful we could add some safe timeout based functions.

comment:4 Changed 8 years ago by Anthony Williams

Resolution: fixed
Status: newclosed

Fixed on trunk.

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain Anthony Williams.
The resolution will be deleted.

Add Comment

E-mail address and name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.