[SOLVED] setExpiryTimeout() not work in QThreadPool.
-
I am a beginner Qt Programmer. I wrote the following code to learn QThreadPool. I set "expiryTimeout" to 3 seconds. "Task" class is a QRunnable inherited class. But every QRunnable thread works over 3 seconds.
@ QThreadPool *glbPool=QThreadPool::globalInstance();
glbPool->setMaxThreadCount(5); glbPool->setExpiryTimeout(3000); task *ts5=new task(5,10000000);@
Task implementation.
@
#include "task.h"
#include <QDebug>
#include <QMutex>
#include <QFile>task::task(int tn, int lc)
{this->taskNo=tn; this->loopCount=lc;
}
task::~task()
{
if(loopIndex==loopCount)
{
qDebug()<<"Task ended. Task Number : "<<this->taskNo<<endl;
}else {
qDebug()<<"Task ended before completion. Task Number : "<<this->taskNo<<endl;
}
}
void task::run()
{
qDebug()<<"Task Started. Task Number : "<<this->taskNo<<endl;
QFile file("out.txt");
if (!file.open(QIODevice::Append | QIODevice::Text))
return;QTextStream * wout= new QTextStream(&file); for(loopIndex=0;loopIndex<this->loopCount;loopIndex++) { this->mutex.lock(); *wout<<"Task "<<this->taskNo<<endl; this->mutex.unlock(); }
}
@ -
According to "expiryTimeout":http://qt-project.org/doc/qt-5/qthreadpool.html#expiryTimeout-prop
"Threads that are unused for expiryTimeout milliseconds are considered to have expired and will exit."
In your example a thread is busy running a loop and will finish only when the loop is complete.