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. QtConcurrent run with a never ending while loop leads to high CPU usage
QtWS25 Last Chance

QtConcurrent run with a never ending while loop leads to high CPU usage

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtconcurrentqueuedconnectiogui signals
17 Posts 7 Posters 2.2k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #6

    Hi,

    @MarKS said in QtConcurrent run with a never ending while loop leads to high CPU usage:

    void MainController::renderFrame(Databuffer frame)
    {
    // process frame and extract all channels
    // lots of computation

       // emit render signals to be displayed on QLabel
       emit renderRGB(channel);
       emit renderConfidence(channel);
       emit renderDepth(channel);      
    

    }

    Why are you emitting three different signals with the exact same data ?
    From the looks of it, you should rather have three slots connected to one single signal.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    MarKSM 1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      @MarKS said in QtConcurrent run with a never ending while loop leads to high CPU usage:

      void MainController::renderFrame(Databuffer frame)
      {
      // process frame and extract all channels
      // lots of computation

         // emit render signals to be displayed on QLabel
         emit renderRGB(channel);
         emit renderConfidence(channel);
         emit renderDepth(channel);      
      

      }

      Why are you emitting three different signals with the exact same data ?
      From the looks of it, you should rather have three slots connected to one single signal.

      MarKSM Offline
      MarKSM Offline
      MarKS
      wrote on last edited by MarKS
      #7

      @SGaist my bad. It was just an example to show that i emit render signals. Of course i am using the right channels to render images. And yes each signal has its own slot.

      Christian EhrlicherC 1 Reply Last reply
      0
      • nageshN Offline
        nageshN Offline
        nagesh
        wrote on last edited by
        #8
        void Controller::play()
        {
            while(m_isPlaying)
            {
                 // get frames from file
                 // update GUI with frame
                 emit showFrameOnWidget(read());                 
                 // update timestamp label
                 emit updateTimestamp(m_timestamp);
            } 
        }
        

        As play() function is part of while loop without sleep how do you ensure fps?
        At present it looks like as soon as data read from file is ready signal is emitted, which is causing high cpu usage.
        Do a comparison of how much time Same file taking to play in VLC and your application?

        jeremy_kJ MarKSM 2 Replies Last reply
        0
        • nageshN nagesh
          void Controller::play()
          {
              while(m_isPlaying)
              {
                   // get frames from file
                   // update GUI with frame
                   emit showFrameOnWidget(read());                 
                   // update timestamp label
                   emit updateTimestamp(m_timestamp);
              } 
          }
          

          As play() function is part of while loop without sleep how do you ensure fps?
          At present it looks like as soon as data read from file is ready signal is emitted, which is causing high cpu usage.
          Do a comparison of how much time Same file taking to play in VLC and your application?

          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by jeremy_k
          #9

          @nagesh said in QtConcurrent run with a never ending while loop leads to high CPU usage:

          As play() function is part of while loop without sleep how do you ensure fps?

          I missed that the source is a file and not a camera producing frames at a fixed rate.

          !

          A timer or blocking IO of some sort is going to be necessary if the goal isn't to be gated by the io, cpu, or memory capacity of the system. Sleeping a fixed interval is a problematic solution because it fails to take into account variability in available capacity. Either set a high precision timer for the desired frame rate, or cause the processing thread to block between each frame while the ui thread deals with it.

          edit: Or use an interface such as QVideoFilterRunnable that is invoked in a manner suitable for playback.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          MarKSM 1 Reply Last reply
          0
          • nageshN nagesh
            void Controller::play()
            {
                while(m_isPlaying)
                {
                     // get frames from file
                     // update GUI with frame
                     emit showFrameOnWidget(read());                 
                     // update timestamp label
                     emit updateTimestamp(m_timestamp);
                } 
            }
            

            As play() function is part of while loop without sleep how do you ensure fps?
            At present it looks like as soon as data read from file is ready signal is emitted, which is causing high cpu usage.
            Do a comparison of how much time Same file taking to play in VLC and your application?

            MarKSM Offline
            MarKSM Offline
            MarKS
            wrote on last edited by
            #10

            @nagesh I think you misunderstood my question. I know to ensure FPS and deal with high CPU usage, I need some way to slow down the thread. I asked for a better way to deal with it rather than using thread sleep which in my opinion, not a good design.

            1 Reply Last reply
            0
            • jeremy_kJ jeremy_k

              @nagesh said in QtConcurrent run with a never ending while loop leads to high CPU usage:

              As play() function is part of while loop without sleep how do you ensure fps?

              I missed that the source is a file and not a camera producing frames at a fixed rate.

              !

              A timer or blocking IO of some sort is going to be necessary if the goal isn't to be gated by the io, cpu, or memory capacity of the system. Sleeping a fixed interval is a problematic solution because it fails to take into account variability in available capacity. Either set a high precision timer for the desired frame rate, or cause the processing thread to block between each frame while the ui thread deals with it.

              edit: Or use an interface such as QVideoFilterRunnable that is invoked in a manner suitable for playback.

              MarKSM Offline
              MarKSM Offline
              MarKS
              wrote on last edited by MarKS
              #11

              @jeremy_k You're right. So, I made a comparison between using a QtConcurrent::run() and QTimer. Here are my observations:

              1. QtConcurrent::run() is very fast. So I used QThread::msleep(mstime). mstime is calculated from the native fps. For e.g. 30 fps native needs 33 mstime between the frames. CPU Usage: ~40-45%

              2. Calling play() using QTimer->start(mstime) where mstime is 33 ms leads to CPU usage to ~30-35%

              It looks like QTimer gives GUI a bit of breather to process events but runs slower than 30 fps playback speed. Whereas, QtConcurrent::run() achieves 30 fps playback speed but drives higher CPU usage.

              It means QTimer sleep interval doesn't guarantee accuracy even though I am using Qt::PreciseTimer. Please correct me if I am wrong. I do not actually understand how the QTimer functions.

              But for now, I am stuck between 2 choices.

              nageshN 1 Reply Last reply
              0
              • MarKSM MarKS

                @jeremy_k You're right. So, I made a comparison between using a QtConcurrent::run() and QTimer. Here are my observations:

                1. QtConcurrent::run() is very fast. So I used QThread::msleep(mstime). mstime is calculated from the native fps. For e.g. 30 fps native needs 33 mstime between the frames. CPU Usage: ~40-45%

                2. Calling play() using QTimer->start(mstime) where mstime is 33 ms leads to CPU usage to ~30-35%

                It looks like QTimer gives GUI a bit of breather to process events but runs slower than 30 fps playback speed. Whereas, QtConcurrent::run() achieves 30 fps playback speed but drives higher CPU usage.

                It means QTimer sleep interval doesn't guarantee accuracy even though I am using Qt::PreciseTimer. Please correct me if I am wrong. I do not actually understand how the QTimer functions.

                But for now, I am stuck between 2 choices.

                nageshN Offline
                nageshN Offline
                nagesh
                wrote on last edited by
                #12

                @MarKS Qt::PreciseTimer guarantees accuracy up to 1msec, but in that slot what operation is performed that matters.

                Calling play() using QTimer->start(mstime) where mstime is 33 ms leads to CPU usage to ~30-35%
                

                Does this timer is running in GUI/main context?

                try to create the worker object having Signal/slot and move it to thread context using movetothread..

                This might reduce the CPU usage

                MarKSM 1 Reply Last reply
                0
                • MarKSM MarKS

                  @SGaist my bad. It was just an example to show that i emit render signals. Of course i am using the right channels to render images. And yes each signal has its own slot.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  @MarKS said in QtConcurrent run with a never ending while loop leads to high CPU usage:

                  And yes each signal has its own slot.

                  This was not what @SGaist said - he said you should not emit three different signals with the same data but only one.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  MarKSM 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @MarKS said in QtConcurrent run with a never ending while loop leads to high CPU usage:

                    And yes each signal has its own slot.

                    This was not what @SGaist said - he said you should not emit three different signals with the same data but only one.

                    MarKSM Offline
                    MarKSM Offline
                    MarKS
                    wrote on last edited by
                    #14

                    @Christian-Ehrlicher I understood. What I meant was 3 different signals are emitted with 3 different channels to 3 different slots. I put the same channel just as an example.

                    1 Reply Last reply
                    0
                    • nageshN nagesh

                      @MarKS Qt::PreciseTimer guarantees accuracy up to 1msec, but in that slot what operation is performed that matters.

                      Calling play() using QTimer->start(mstime) where mstime is 33 ms leads to CPU usage to ~30-35%
                      

                      Does this timer is running in GUI/main context?

                      try to create the worker object having Signal/slot and move it to thread context using movetothread..

                      This might reduce the CPU usage

                      MarKSM Offline
                      MarKSM Offline
                      MarKS
                      wrote on last edited by
                      #15

                      @nagesh yes the timer is created in GUI thread. Never tried QTimer in another thread. Will search how to do that. But if you have any pointers that would be helpful.

                      nageshN kshegunovK 2 Replies Last reply
                      0
                      • MarKSM MarKS

                        @nagesh yes the timer is created in GUI thread. Never tried QTimer in another thread. Will search how to do that. But if you have any pointers that would be helpful.

                        nageshN Offline
                        nageshN Offline
                        nagesh
                        wrote on last edited by
                        #16

                        @MarKS I would suggest first example provided here
                        https://doc.qt.io/qt-5/qthread.html

                        You can use worker objects by moving them to the thread using QObject::moveToThread().
                        

                        In worker object create timer.

                        1 Reply Last reply
                        1
                        • MarKSM MarKS

                          @nagesh yes the timer is created in GUI thread. Never tried QTimer in another thread. Will search how to do that. But if you have any pointers that would be helpful.

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #17

                          What is DataBuffer? And how do you render the data in the GUI thread?

                          Read and abide by the Qt Code of Conduct

                          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