PDA

View Full Version : QtConcurrent Threads



mlheese
30th July 2010, 05:40
I'm using a QtConcurrent::run() thread for 20 threads that run at the same time as shown below. We have 20 network devices that run embedded Linux and this code loop fires a thread that uses LIBSSH2 to connect to each device and run some scripts. I'm also using Signals/Slots (not shown) to communicate status data between the main app and each Thread.

My question is about how QtConcurrent uses threads. The documentation shows that each thread is taken from the global QThreadPool. In this class, there is a function that returns idealThreadCount() based on my processor. I'm wondering if it is recommended to use setMaxThreadCount() to set the Max Thread count to, say 5 or 10 if the idealThreadCount() returned is only 2? My 2.4GHz Centrino laptop (2GBram) returns a value of 2 for the idealThreadCount(). I've noticed that by setting this max value to 5 on my laptop causes my laptop to drag and the threads are just crawling but they are running.

Just wondering if I should maybe limit the maximum thread count to less than or equal to the idealThreadCount() since I never know what type of PC the code will run on?? Or am I confusing this value? I certainly don't want a user's PC to come to a crawling standstill if I set the count too high to 20.

Any suggestions?

code snippet:
for(int i = 0; i < 20; i++ )
{
QFuture<void> future = QtConcurrent::run(this, &QMainSubclassForm::MyThreadFunc,param1);
m_FutureWatcher->setFuture(future);
}

tbscope
30th July 2010, 06:35
The name of the function idealThreadCount() should really be amountOfCpuCores()

It just returns the amount of cores on your pc. In your case 2.
Obviously, you can run more threads than that.

Of course, if each and every thread requires 100% cpu time, the amount of cores really becomes the ideal thread count.