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. [SOLVED]Is there a problem with the following non-blocking sleep function?
Forum Update on Monday, May 27th 2025

[SOLVED]Is there a problem with the following non-blocking sleep function?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 6.9k 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.
  • A Offline
    A Offline
    antonyprojr
    wrote on last edited by
    #1

    @
    QEventLoop loop;
    QTimer::singleShot(time_to_wait, &loop, SLOT(quit()));
    loop.exec();
    @

    I'm using this approach to create a wait function that does not freeze the window, in my tests it's working! So what I want to know is if this is a good approach or not.

    1 Reply Last reply
    1
    • A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      Looks good to me. :)

      Not sure why you would want to non-block sleep a thread but it would accomplish what you want.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      0
      • A Offline
        A Offline
        antonyprojr
        wrote on last edited by
        #3

        Thanks for your answer! :)

        An explanation: I need this because I need to check a value of a variable using while and only goes away when the desired value is got, also I need my thread free to handle some signals during the loop execution(the condition expected by the while is got when some signal is triggered and processed) and when the desired value is got, finally the loop ends.

        And I need the loop to prevent the class goes away, btw the function is the class destructor.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          Interesting design idea..

          Could you not just send a signal your class catches when the value is done being updated and then that slot could call this->deleteLater() thus cleaning itself up?

          It seems like the while loop is an extreme way to check that variable. Obviously hard on your cpu which is why you need that sleep.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RJV B
            wrote on last edited by
            #5

            I came across this old topic looking if there was a ready-made solution to "active-but-not-busy-wait", i.e. a function or class that works like sleep() but without the blocking aspect. IOW, not a real sleep, more like a slumber :)

            Here's my take based on the OP's solution:

            class QSlumber
            {
            public:
                QSlumber(qreal seconds)
                {
                    if (seconds > 0) {
                        QEventLoop loop;
                        QTimer::singleShot(seconds * 1000, &loop, SLOT(quit()));
                        loop.exec();
                    }
                }
                static qreal during(qreal seconds)
                {
                    QElapsedTimer duration;
                    duration.start();
                    QSlumber sleep(seconds);
                    return duration.elapsed() / 1000.0;
                }
            };
            
            1 Reply Last reply
            1

            • Login

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