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]- QThread - QEventLoop vs Sleep
Forum Update on Monday, May 27th 2025

[SOLVED]- QThread - QEventLoop vs Sleep

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

    I have a Thread starting from my QMainWindow that has a busy loop (execute until MainWindow ask him to close)
    So far I am running this loop this way :

    @ /// Main Loop
    while (antThreadRunning) {
    QThread::msleep(150);
    }@

    I was wondering if a QEventLoop would be more efficient
    For example:

    @ /// Main Loop, done in .exec of QEventLoop
    QEventLoop loop;
    QObject::connect(thread, SIGNAL(antThreadRunningFinish()), &loop, SLOT(quit()) );
    loop.exec();@

    By calling loop.exec, what is actually happening? is it an infinite loop that use lot of CPU? I would like to find the faster/better approach between the two, thank you!


    Free Indoor Cycling Software - https://maximumtrainer.com

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

      QEvenLoop is more efficient then busy loop, it does not eat CPU like busy loop, and it allows the thread to process the signals and events.

      If you need to wait for some predefined time then you can use something like this
      @
      QEventLoop loop;
      QTimer timer;
      QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
      int runTime = seconds * 1000;
      timer.start(runTime);
      loop.exec();
      @

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        Thanks for the info andreyc,

        I'll replace all my busy loop with QEventLoop, I discover new awesome things in Qt every day!

        @ /// Loop until channel closed or maximum of 25sec
        QEventLoop loop;
        QTimer timer;
        timer.setSingleShot(true); // single serving timer...
        timer.start(25 * 1000); // 25sec

        QObject::connect(this, SIGNAL(signal_allChannelClosed()), &loop, SLOT(quit()) );
        QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
        
        loop.exec();
        
        qDebug() << "loop done!";@
        

        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mhennings
          wrote on last edited by
          #4

          [quote author="maximus" date="1406208599"]
          [...]
          I'll replace all my busy loop with QEventLoop
          [...]
          [/quote]

          Beware though that "nested event loops can have unexpected and undesired side effects.":http://delta.affinix.com/2006/10/23/nested-eventloops/

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maximus
            wrote on last edited by
            #5

            Thanks for the heads up!
            It's all good the code in executed during the EventLoop is all code I control and no nested event-looping is done ;)


            Free Indoor Cycling Software - https://maximumtrainer.com

            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