public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/58038] New: std::this_thread::sleep_until can cause inifinite sleep
@ 2013-07-31 13:27 mario.bielert@tu-dresden.de
  2013-07-31 14:02 ` [Bug libstdc++/58038] " redi at gcc dot gnu.org
                   ` (15 more replies)
  0 siblings, 16 replies; 17+ messages in thread
From: mario.bielert@tu-dresden.de @ 2013-07-31 13:27 UTC (permalink / raw)
  To: gcc-bugs

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.


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2015-04-11 11:56 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-31 13:27 [Bug libstdc++/58038] New: std::this_thread::sleep_until can cause inifinite sleep mario.bielert@tu-dresden.de
2013-07-31 14:02 ` [Bug libstdc++/58038] " redi at gcc dot gnu.org
2013-07-31 14:17 ` redi at gcc dot gnu.org
2013-07-31 14:35 ` redi at gcc dot gnu.org
2013-07-31 14:37 ` paolo.carlini at oracle dot com
2013-11-18 11:24 ` mario.bielert@tu-dresden.de
2013-11-18 11:42 ` redi at gcc dot gnu.org
2014-02-28  7:38 ` m at matthewlai dot ca
2014-05-14 20:37 ` m at matthewlai dot ca
2014-09-16  4:11 ` dan at stahlke dot org
2014-09-16  8:17 ` redi at gcc dot gnu.org
2015-03-26 20:06 ` redi at gcc dot gnu.org
2015-03-26 20:09 ` redi at gcc dot gnu.org
2015-03-26 20:09 ` redi at gcc dot gnu.org
2015-03-26 20:27 ` m at matthewlai dot ca
2015-04-11 11:47 ` redi at gcc dot gnu.org
2015-04-11 11:56 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).