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 queries....is 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. -
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));
@ -
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...
-
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
}@