Reputation: 549
Im developing an application in Qt which, at some point, process a bunch of videos. It works fine but it had only a 40-60% of the cpu usage during the process phase so i tried to make it multithreaded.
I used QtConcurrent cause his 'high leveness' instead a more traditional thread management, my code is simply:
for(int i = 0; i < totalVideos; i++)
{
QFuture<ResultClass *> futureToken = QtConcurrent::run(this, process, listOfVideos.takeFirst());
QFutureWatcher<ResultClass *>* fw = new QFutureWatcher<ResultClass *>();
connect(fw, SIGNAL(finished()), this, SLOT(manageResult));
fw->setFuture(futureToken);
}
aaaand it works, 100% cpu usage and its around 25-30% faster. But it spawns around 65 new threads (regardless it process 25 or 250 videos) and most of those threads doesn't disappear after the process phase.
My question is: Is this approach right? Is it too raw? Should i control 'manually' the thread creation? Does the QtConcurrent module takes care of all so i should not care of the thread management? Are 85 threads too much? Should i try to kill some of those after the process phase??
Every observation has been done just looking at the Activity monitor.
Thanks in advance.
Upvotes: 5
Views: 2947
Reputation: 27631
The future of QtConcurrent seems to be uncertain if you read this thread.
Having more threads that processing cores is somewhat redundant. If you have one core and 2 threads running, the processor spends time switching between the processing the 2 threads, but gives the user the appearance of simultaneous processing.
With the same number of cores and threads, the threads can be split between the cores.
Once you have more cores than threads, you're back to the original method of cores jumping up and back between the threads that it is required to process.
Using QThread is actually very easy to do as QThread is not directly a thread, but a thread controller. You can read about how to 'really truly use QThreads' here.
As it describes, you create objects inherited to QObject and move that to a QThread. What is rarely mentioned is that you can move multiple objects to the new QThread, if required.
Upvotes: 6