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.
Qt offers many classes and functions for working with threads. Below are four different approaches that Qt programmers can use to implement multithreaded applications.
QThread::QThread(QObject *parent = nullptr) Constructs a new QThread to manage a new thread. The parent takes ownership of the QThread. The thread does not begin executing until start() is called. See also start().
The Qt Concurrent module provides high-level APIs that make it possible to write multi-threaded programs without using low-level threading primitives such as mutexes, read-write locks, wait conditions, or semaphores.
To run a function in another thread, use QtConcurrent::run(): extern void aFunction(); QFuture<void> future = QtConcurrent::run(aFunction); This will run aFunction in a separate thread obtained from the default QThreadPool. You can use the QFuture and QFutureWatcher classes to monitor the status of the function.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With