Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThread::sleep() question

QThread::sleep() question

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 23.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DRoscoe
    wrote on last edited by
    #1

    There are three variants of QThread::sleep():

    QThread::sleep(); // seconds
    QThread::msleep(); // msec
    QThread::usleep(); // usec
    

    Are these merely convenience forms allowing you to specify arbitrary sleep durations in these units, or is the input argument assumed to be within a range? For example, if is use:

    quint sleep_msec = 6,000;
    QThread::msleep(sleep_msec );
    

    Will this work without an overflow since the sleep_msec value is greater than one second?

    The documentation doesn't specify.

    Thanks!

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi! For Unix-like systems, the definition is in qthread_unix.cpp:

      void QThread::sleep(unsigned long secs)
      {
          qt_nanosleep(makeTimespec(secs, 0));
      }
      
      void QThread::msleep(unsigned long msecs)
      {
          qt_nanosleep(makeTimespec(msecs / 1000, msecs % 1000 * 1000 * 1000));
      }
      
      void QThread::usleep(unsigned long usecs)
      {
          qt_nanosleep(makeTimespec(usecs / 1000 / 1000, usecs % (1000*1000) * 1000));
      }
      

      qt_nanosleep is defined in qelapsedtimer_unix.cpp:

      void qt_nanosleep(timespec amount)
      {
          // We'd like to use clock_nanosleep.
          //
          // But clock_nanosleep is from POSIX.1-2001 and both are *not*
          // affected by clock changes when using relative sleeps, even for
          // CLOCK_REALTIME.
          //
          // nanosleep is POSIX.1-1993
      
          int r;
          EINTR_LOOP(r, nanosleep(&amount, &amount));
      }
      

      So all three functions call nanosleep(2) in the end.

      Addendum:
      makeTimespec is also defined in qthread_unix.cpp:

      static timespec makeTimespec(time_t secs, long nsecs)
      {
          struct timespec ts;
          ts.tv_sec = secs;
          ts.tv_nsec = nsecs;
          return ts;
      }
      
      1 Reply Last reply
      2
      • D Offline
        D Offline
        DRoscoe
        wrote on last edited by
        #3

        Excellent! Thank you! I really should download the source code for Qt ;-)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved