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::map uses 100% of CPU
QtWS25 Last Chance

QtConcurrent::map uses 100% of CPU

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 5.5k 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.
  • G Offline
    G Offline
    gronerth
    wrote on last edited by
    #1

    Hello!!

    I was implementing a project where i have to do snmp queries periodically (it's up to the interval defined). Because, it could take too much time to do all the queries, i am using QtConcurrent::map to do the queries in another thread but i can see that the cpu reaches 100% while the program it's doing the queries....is it normal that the cpu reaches 100% when you use threads? how can you avoid that?

    Thanks in advance..


    JETG

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      If threads are doing nothing they should require 0% cpu.
      But the point is: what is doing nothing?

      If you create your own threads, yo normally wait on wait conditions. This resumes the thread and lets it sleep till the condition is meat.
      Regarding QtConcurrent, it means it depends what your function is doing.
      Please show us some code, then we can perhaps answer your question.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZapB
        wrote on last edited by
        #3

        As Gerolf said it depends upon what you are asking to be done in the worker threads. Post some code...

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gronerth
          wrote on last edited by
          #4

          Well, i have the function "executeAllActiveTasks" that takes a string as an argument and calls a function "requestandsaveData", this function does a snmp-get to an specific device and waits until the data is available and then saves the data in a file. The following is the code for "executeAllActiveTasks"

          @void executeAllActiveTasks(QString tname)
          {
          // double val;
          QDir path( "./databases/" );

          task *t = tasks.value(tname);
          RCDevice *d = devices.value(t->device_id());
          
                 if(t!=NULL || t->status()!=STARTED || d!=NULL)
                 {
                     Metrics.value(t->metricType())->requestAndSaveData(d,path.absolutePath()+ QString("/") +t->taskName()+QString(".rrd"),t->metricType(),t->metricParams(),0);
                 }
          

          }@

          I call that function with the following code, where "taskActivesByThread" it is a QStringList...

          @watcher.setFuture(QtConcurrent::map(tasksActivesByThread,executeAllActiveTasks));
          @


          JETG

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            how long running is his task?

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • G Offline
              G Offline
              gronerth
              wrote on last edited by
              #6

              It is up to the size of the "taskActivesByThread" and the time to get the response of the devices, it could be one minute or even more. The application works fine, but I just want to know if any other alternative methods would produce 100% of CPU while the polling is active...


              JETG

              1 Reply Last reply
              0
              • B Offline
                B Offline
                baysmith
                wrote on last edited by
                #7

                Possibly something is in an active wait state, meaning it is constantly querying to see if the results are ready.

                Nokia Certified Qt Specialist.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gronerth
                  wrote on last edited by
                  #8

                  Well, yesss..they are waiting for the results to be ready, but just because a thread is in an active state will produce a huge increment in the cpu?


                  JETG

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    baysmith
                    wrote on last edited by
                    #9

                    If the thread is in an active wait state (for example waiting for a spin lock), yes. If the thread is in an inactive wait state (for example waiting for a conditional variable), no.

                    What does snmp-get do and is it thread safe?

                    Nokia Certified Qt Specialist.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gronerth
                      wrote on last edited by
                      #10

                      It is thread safe...the snmp get is really a "QProcess" calling the libraries of the net-snmp, so i need the wait until the "QProcess" finish to captutre the data...


                      JETG

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        baysmith
                        wrote on last edited by
                        #11

                        How is it waiting for the QProcess to finish and how is it capturing the data?

                        Nokia Certified Qt Specialist.

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          gronerth
                          wrote on last edited by
                          #12

                          It's something like this...

                          @
                          QProcess snmpGet;
                          snmpGet.start(program,params);

                          if (snmpGet.waitForStarted())
                          {
                          if (!snmpGet.waitForFinished())
                          {
                          return INVALID_VALUE;
                          }
                          QString output(snmpGet.readAll());
                          //Then i have to parsing the data to obtain the data...

                          }else{

                          return INVALID_VALUE
                          }

                          @


                          JETG

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            baysmith
                            wrote on last edited by
                            #13

                            Nothing seems wrong with what you've described so far. If you interrupt it in a debugger when the CPU is at 100%, where does it break?

                            Nokia Certified Qt Specialist.

                            1 Reply Last reply
                            0
                            • Z Offline
                              Z Offline
                              ZapB
                              wrote on last edited by
                              #14

                              Profile it and see where the profiler says the cpu is spending all of its time.

                              Nokia Certified Qt Specialist
                              Interested in hearing about Qt related work

                              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