Details
-
Bug
-
Resolution: Done
-
P2: Important
-
5.15.3, 5.15.4
-
None
-
Windows 10
macOS 10.15
-
-
21
-
2a3425cb49eff0753feae3b41bc36655e30b0372 (qt/tqtc-qtbase/5.15)
-
Qt6_Foundation_Sprint 36, Qt6_Foundation_Sprint 37
Description
By default, QThread::idealThreadCount() and QThreadPool::globalInstance()->maxThreadCount() match the number of logical cores.
The documentation says that "maxThreadCount represents the maximum number of threads used by the thread pool".
I have attached a simple test project that uses the threadpool through QtConcurrent::blockingMap and counts the number of unique threadIds used and the number of times each thread was called. The resulting thread count is always one higher than the specified max number of threads.
So on my 8 core machine I get 9 threads, on a 12 core I get 13 threads, etc. Results confirmed on Windows and macOS. Somehow the logic in QThreadPool gets it wrong by one.
Here is a sample output of my test program:
Thread 1: 9273 hits
Thread 2: 8841 hits
Thread 3: 8841 hits
Thread 4: 8848 hits
Thread 5: 8837 hits
Thread 6: 8837 hits
Thread 7: 8845 hits
Thread 8: 8837 hits
Thread 9: 8841 hits
Sum: 80000
idealThreadCount() : 8
maxThreadCount() : 8
actual Thread count: 9
It is supposed to create 8 threads with 10,000 hits each. Instead I get 9 threads with about 9,000 hits each. If I set the maximum number of threads manually, the result is also always one more thread than expected.