Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QtConcurrent::map uses 100% of CPU

    General and Desktop
    4
    14
    4472
    Loading More Posts
    • 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
      gronerth last edited by

      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 Reply Quote 0
      • G
        giesbert last edited by

        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 Reply Quote 0
        • Z
          ZapB last edited by

          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 Reply Quote 0
          • G
            gronerth last edited by

            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 Reply Quote 0
            • G
              giesbert last edited by

              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 Reply Quote 0
              • G
                gronerth last edited by

                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 Reply Quote 0
                • B
                  baysmith last edited by

                  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 Reply Quote 0
                  • G
                    gronerth last edited by

                    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 Reply Quote 0
                    • B
                      baysmith last edited by

                      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 Reply Quote 0
                      • G
                        gronerth last edited by

                        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 Reply Quote 0
                        • B
                          baysmith last edited by

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

                          Nokia Certified Qt Specialist.

                          1 Reply Last reply Reply Quote 0
                          • G
                            gronerth last edited by

                            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 Reply Quote 0
                            • B
                              baysmith last edited by

                              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 Reply Quote 0
                              • Z
                                ZapB last edited by

                                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 Reply Quote 0
                                • First post
                                  Last post