Opened 13 years ago

Closed 12 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:

Description

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.

Change History (4)

comment:1 by viboes, 13 years ago

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

comment:2 by Marshall Clow, 13 years ago

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)
        {
            this_thread::sleep(get_system_time()+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 by viboes, 12 years ago

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 by Anthony Williams, 12 years ago

Resolution: fixed
Status: newclosed

Fixed on trunk.

Note: See TracTickets for help on using tickets.