[Qt-interest] Some thoughts about QThread::idealThreadCount()

Yves Bailly yves.bailly at sescoi.fr
Thu Mar 5 11:10:52 CET 2009


Hello (ex)Trolls and all,

While doing some multi-threading work, I came to think about the
meaning of the static method QThread::idealThreadCount().

Short version: in my humble opinion, this method is wrongly named,
a better name would be "numberOfCores()" or something like that.

Long version.
It seems this method just returns the number of cores found on the
system (at least in the very few tests I made). Say I have a dual-
core CPU: this method returns 2. So, if I use QThreadPool, it will
allow me to run simultaneously only 2 QRunnable, queuing the extra
ones I might require (assuming I understand the doc correctly).

But wait... creating and running 2 QRunnable will give me *3* threads:
the two I created, *and* the main application thread... this is more
than "idealThreadCount()", so it's not ideal, so I'll loose perfs... ?

Another approach. Consider an algorithm that bounce everywhere in a
huge memory array, for example a simple QuickSort implementation. This
type of algorithm will cause many cache misses on the processor, thus
the process will spend most of its time waiting for data to be fetched
from main memory.
I did some benchmarks, and I discovered that even on a single-CPU as
"old" as a Pentium-4, threading a QuickSort can bring significant gains
in speed (I tried up to 8 threads, gaining overall more than 40%). This
just because while a thread waits for its data to be fetched, another
can work. So in this case, whereas "idealThreadCount()" would probably
return 1 (haven't tried yet), my "ideal thread count" for my specific
problem would be around 8.

Conclusion: having a way to know the number of cores in a systems is very
interesting, thanks for providing us with a so useful method. But naming
it "idealThreadCount()" does not seem really correct to me, at least from
a semantic point of view.

OK, that was a long post about a non-technical problem :-) And maybe
I missed or misunderstood something. If so, sorry for the trouble, please
enlighten me (english not being my native language).

Regards,

-- 
     /- Yves Bailly - Software developper  -\
     \- Sescoi France http://www.sescoi.fr -/
"The possible is done. The impossible is being done. For miracles,
thanks to allow a little delay."




More information about the Qt-interest-old mailing list