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

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.
  • N Offline
    N Offline
    nagesh
    wrote on 19 Mar 2021, 23:08 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?

    J M 2 Replies Last reply 20 Mar 2021, 06:10
    0
    • N nagesh
      19 Mar 2021, 23:08
      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?

      J Offline
      J Offline
      jeremy_k
      wrote on 20 Mar 2021, 06:10 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/

      M 1 Reply Last reply 20 Mar 2021, 08:55
      0
      • N nagesh
        19 Mar 2021, 23:08
        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?

        M Offline
        M Offline
        MarKS
        wrote on 20 Mar 2021, 08:36 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
        • J jeremy_k
          20 Mar 2021, 06:10

          @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.

          M Offline
          M Offline
          MarKS
          wrote on 20 Mar 2021, 08:55 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.

          N 1 Reply Last reply 20 Mar 2021, 09:11
          0
          • M MarKS
            20 Mar 2021, 08:55

            @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.

            N Offline
            N Offline
            nagesh
            wrote on 20 Mar 2021, 09:11 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

            M 1 Reply Last reply 20 Mar 2021, 13:36
            0
            • M MarKS
              19 Mar 2021, 22:52

              @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.

              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 20 Mar 2021, 09:38 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

              M 1 Reply Last reply 20 Mar 2021, 13:32
              0
              • C Christian Ehrlicher
                20 Mar 2021, 09:38

                @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.

                M Offline
                M Offline
                MarKS
                wrote on 20 Mar 2021, 13:32 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
                • N nagesh
                  20 Mar 2021, 09:11

                  @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

                  M Offline
                  M Offline
                  MarKS
                  wrote on 20 Mar 2021, 13:36 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.

                  N K 2 Replies Last reply 20 Mar 2021, 14:17
                  0
                  • M MarKS
                    20 Mar 2021, 13:36

                    @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.

                    N Offline
                    N Offline
                    nagesh
                    wrote on 20 Mar 2021, 14:17 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
                    • M MarKS
                      20 Mar 2021, 13:36

                      @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.

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 20 Mar 2021, 20:38 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

                      17/17

                      20 Mar 2021, 20:38

                      • Login

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