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. Keep UI responsive when CPU load is 100%
QtWS25 Last Chance

Keep UI responsive when CPU load is 100%

Scheduled Pinned Locked Moved General and Desktop
12 Posts 6 Posters 6.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.
  • S Offline
    S Offline
    SaiyanRiku
    wrote on last edited by
    #1

    I have a multi-threaded Qt application. Worker threads received data and process them as fast as possible.

    Nevertheless, when the amount of data is too big for the machine, the CPU is used at 100%, and the Qt UI become lagging. It can takes 5 seconds to get a button click performed.

    I tried to set the workers threads priority to QThread::LowestPriority, but it is changing nothing.

    Is there any easy way to tell to my program to keep some CPU load available for the UI (and the OS). In fact, i want the worker threads working fully only if there is no other tasks to do on the computer.

    Thank you for your help.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pushpendrak
      wrote on last edited by
      #2

      for my perception during the Mouse events you may pause or slow down (by increasing the delay inside thread) the thread, by this you may have enough CPU cycle for the UI response.
      it is always better to give some delay inside thread instead of continuous loop.

      @
      {
      //process some data
      sleep(10);
      }
      @

      instead of--
      @
      {
      //process some data
      ......................
      }@

      PushpendraK

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

        Are you sure all your objects are in the thread you think they are? Could it be that your TCP handing is in the main thread after all, thus blocking GUI handling with both TCP work and the work to do the cross-thread signalling needed?

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

          Using just sleep() function will still block UI, you need to process pending events to:

          @
          {
          //process some data
          QApplication::processEvents(QEventLoop::AllEvents);
          sleep(10);

          }@

          God is Real unless explicitly declared as Integer.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            [quote author="AcerExtensa" date="1342434829"]Using just sleep() function will still block UI, you need to process pending events to:

            @
            {
            //process some data
            QApplication::processEvents(QEventLoop::AllEvents);
            sleep(10);

            }
            @
            [/quote]

            We're talking multi-threading here, so no, sleep will not block your entire application.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              AcerExtensa
              wrote on last edited by
              #6

              right, sorry...

              God is Real unless explicitly declared as Integer.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SaiyanRiku
                wrote on last edited by
                #7

                Thank you for all you're answer.

                Actually, I would like to not use a sleep function because my application is rendering frame in multiple view at same time (each thread is one view). The GUI is Qt but the frame rendering is done by DirectX in the worker thread. This is done to allow the Qt UI Thread to only manage the Qt's GUI events. So i want the frame rendering to be fast as it is possible to keep the original FPS (when it's possible), and sleep (10) can disturb the rendering.

                By the way, normally my worker thread doesn't use any signal, the thread is a QThread but it doesn't use any other Qt Object (in fact only QSharedPointer).

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Asperamanca
                  wrote on last edited by
                  #8

                  You'll have to decide between a responsive UI and maxing out the CPU with your worker threads, to a point.

                  If 10 milliseconds sounds too long, you might take a look at QThread::usleep()

                  And by the way: If sleep(10) can really disturb the rendering (and not only delay it slightly), then you are in trouble. Because you don't know how long it will take for the context to return to any specific worker thread. It might be way beyond 10 milliseconds - you just don't know. No logic in your code can assumes there's no break in execution longer than that.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SaiyanRiku
                    wrote on last edited by
                    #9

                    So i tried the sleep this solution, but it doesn't work. I think it's because if i have for example 4 thread, they basically never fall in sleep at the same time, so the CPU load is always saturated.

                    Of course if i use sleep(500), ok it works, but the FPS fall to 2 FPS (to small). Using sleep(10) is not enough, to give time to the UI thread, and if i want to keep at least 24 FPS (i can't use more that sleep 30).

                    Really no way to tell the OS that some thread are secondary and you have to give time to it, only if there is nothing else to do?

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      pushpendrak
                      wrote on last edited by
                      #10

                      Actually you not told us how ur application works, as if u r using DirectX, may be it is SW rendering no gpu, check ot first. Because if app is running as sw rendered then it will eat up ur cpu.

                      PushpendraK

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SaiyanRiku
                        wrote on last edited by
                        #11

                        Hi, thank you all for your help! I found the solution. Sometime the problem is between the screen and the chair. It was just a bug in my application.

                        I investigated why thread priority was not working, and using process explorer i found that my priority wasn't really set. I was just an hidden line of code that was changing the priority. So setting the thread priority to low work very well.

                        My problem is solved!

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          rcari
                          wrote on last edited by
                          #12

                          You should also add a @QThread::yieldCurrentThread()@ in the inner-loops of the threads that perform the background operations. This will give the OS a hint that it is a good time to reschedule CPU time among all the threads.

                          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