QThreadPool after each cycle slows down
-
Hello,
I've been stuck for couple days on QThreadPool slowing down after each cycle.
Basically, I'm developing simulation. where each QRunnable is responsible for some amount of nodes. Because of that, I don't want to delete the tasks, but just want to update one variable, and rerun all of them. It kinda works, but after each cycle it slows down.I managed to reproduce the same results, by simplifying the simulation cycle, it consists of the basic building blocks.
I initialize QThreadPool
Construct a QList of my QRunnables "XThread"
Then I simulate 5000 simulation cycles
Were in each cycle I 'restart' all the XThreads
And wait until all of them are done
However after each cycle, the computation time increases, I don't understand why? I tried clearing the QThreadPool, creating a new one, still the same problem
Here are my test results, where I print the execution time every 50 simulation cycles.
On the left simulation cycle, on the right execution time in seconds@[13:35:14 - 16/06/2014] 0 0.002
[13:35:14 - 16/06/2014] 50 0.19
[13:35:15 - 16/06/2014] 100 0.161
[13:35:15 - 16/06/2014] 150 0.153
[13:35:15 - 16/06/2014] 200 0.156
[13:35:15 - 16/06/2014] 250 0.161
[13:35:15 - 16/06/2014] 300 0.166
[13:35:15 - 16/06/2014] 350 0.166
[13:35:16 - 16/06/2014] 400 0.167
[13:35:16 - 16/06/2014] 450 0.173
[13:35:16 - 16/06/2014] 500 0.176
[13:35:16 - 16/06/2014] 550 0.203
[13:35:16 - 16/06/2014] 600 0.23
[13:35:17 - 16/06/2014] 650 0.211
[13:35:17 - 16/06/2014] 700 0.2
[13:35:17 - 16/06/2014] 750 0.212
[13:35:17 - 16/06/2014] 800 0.222
[13:35:17 - 16/06/2014] 850 0.226
[13:35:18 - 16/06/2014] 900 0.221
[13:35:18 - 16/06/2014] 950 0.233
[13:35:18 - 16/06/2014] 1000 0.261@Here is the code, for initializing the XThreads and starting them in simulated simulation cycle:
@qDebug() << "Testing threads!";
QThreadPool * pool = new QThreadPool(this);QElapsedTimer counter; counter.start(); QElapsedTimer timer2; timer2.start(); QList<XThread*> threads; //Lets make 50 "heavy" tasks for(int i=0; i<50; i++) { XThread* th = new XThread(XThread::TEST); threads.push_back(th); } //In my simulation, I have to reexecute the tasks couple 1000 times, even millions for(int k=0; k<5000; k++) { foreach(XThread* th, threads) { pool->start(th); } if(k mod 50 == 0) { QCoreApplication::processEvents(); qDebug() << k << double(timer2.elapsed())/1000.0; timer2.restart(); } pool->waitForDone(); } qDebug() << "Time it took to execute (seconds): " << double(counter.elapsed())/1000.0; qDebug() << "Thread test finished";@
And here is XThread implementation
@XThread::XThread(ThreadMode mode) :
QRunnable()
{
num = 0;
this->mode = mode;
this->setAutoDelete(false);
}void XThread::run()
{
if(mode == TEST)
{
for(int i=0; i<100000; i++);
}
}@I wouldn't be surprised, I'm missing something simple.
Kind Regards
Raivis