This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/58038] New: std::this_thread::sleep_until can cause inifinite sleep


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58038

            Bug ID: 58038
           Summary: std::this_thread::sleep_until can cause inifinite
                    sleep
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mario.bielert@tu-dresden.de

Created attachment 30576
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=30576&action=edit
Small test case, build with g++ -std=c++0x

When using sleep_until() I get an bug with unsigned long scalar representations
of a duration. If this duratoiin is in past, then you get an overflow in the
length of the argument for sleep_for(). This causes an almost infinte sleep,
instead of a fast return.

I solved this with defining two seperate implementions for sleep_until using
enable_if.

#################################################

    /// sleep_until for signed representations
    template<typename _Clock, typename _Duration>
    inline
    typename std::enable_if<std::is_signed<typename _Duration::rep>::value,
void>::type
    sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
    {
        sleep_for(__atime - _Clock::now());
    }

    /// sleep_until for unsigned representations
    template<typename _Clock, typename _Duration>
    inline
    typename std::enable_if<std::is_unsigned<typename _Duration::rep>::value,
void>::type
    sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
    {
        typename _Clock::time_point _now = _Clock::now();
        // check if we should sleep till a time point in past
        if(__atime > _now)
            // if not, procede as usual
            sleep_for(__atime - _now);
    }

#################################################

Sorry for not providing a .patch file, as I'm hacked my local installed
headers.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]