Important: Please read the Qt Code of Conduct -

QtConcurrent::map uses 100% of CPU

  • 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 it normal that the cpu reaches 100% when you use threads? how can you avoid that?

    Thanks in advance..

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

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

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


  • how long running is his task?

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

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

  • 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?

  • 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?

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

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

  • It's something like this...

    QProcess snmpGet;

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


    return INVALID_VALUE


  • 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?

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

Log in to reply