I am trying to implement the multi threading programming.
Prob: I need to read huge data from Db (Which will be done through 3rd party API for which i don't have control), So my plan is so create ideal number of threads (max 5)
and each thread reads chunk of data at the end all the chunks will be merged. (I should be able to cancel the process in middle if I don't want).
I just tried below sample code(Before going to above implementation), when I click on cancel button It's slot is not get called, instead slot will be getting called after all the threads finished.
// I am just doing some busy work here As I don't know what is happening here, this function is not in my control
QVector<int> thirdPartyFunction()
{
int t1 = time.elapsed()/1000;
while( ((time.elapsed()/1000) - t1) < 3)
{
}
return QVector<int>();
}
QVector<int> myThread()
{
return thirdPartyFunction();
}
{
progress
= new QProgressDialog("Threading in progress",
"Cancel",
0,
100,
this);
connect(progress, SIGNAL(canceled()), this, SLOT(cancelAllThreads()), Qt::QueuedConnection);
progress->setValue(0);
// starting 5 threads
for(int i=0; i< 5; i++)
{
synch.addFuture(QtConcurrent::run(myThread));
progress->setValue(progress->value() + 10);
}
synch.waitForFinished();
QVector<int> finalList;
if(synch.cancelOnWait())
{
qDebug() << "cancel all the threads......";
}
else
{
//if I never press cancel
QList<QFuture< QVector<int> > > futuresList = synch.futures();
for ( int i = 0; i < futuresList.size(); i++ )
{
QVector<int> list = futuresList.at(i);
finalList << list;
progress->setValue(progress->value() + 10);
qDebug() << "pro val = " << progress->value() + 10;
}
qDebug() << "All threads finished successfully - size" << finalList.size();
}
}
Dialog::~Dialog()
{
}
//this is not getting called as soon as I licked cancel
void Dialog::cancelAllThreads()
{
//here I want to cancel & clean up all the threads
qDebug() << "Canceled pressed....";
synch.setCancelOnWait(true);
}
// I am just doing some busy work here As I don't know what is happening here, this function is not in my control
QVector<int> thirdPartyFunction()
{
QTime time;
int t1 = time.elapsed()/1000;
while( ((time.elapsed()/1000) - t1) < 3)
{
}
return QVector<int>();
}
QVector<int> myThread()
{
return thirdPartyFunction();
}
Dialog::Dialog(QWidget *parent) : QDialog(parent)
{
progress = new QProgressDialog("Threading in progress", "Cancel", 0, 100, this);
connect(progress, SIGNAL(canceled()), this, SLOT(cancelAllThreads()), Qt::QueuedConnection);
progress->setValue(0);
// starting 5 threads
for(int i=0; i< 5; i++)
{
synch.addFuture(QtConcurrent::run(myThread));
progress->setValue(progress->value() + 10);
}
synch.waitForFinished();
QVector<int> finalList;
if(synch.cancelOnWait())
{
qDebug() << "cancel all the threads......";
}
else
{
//if I never press cancel
QList<QFuture< QVector<int> > > futuresList = synch.futures();
for ( int i = 0; i < futuresList.size(); i++ )
{
QVector<int> list = futuresList.at(i);
finalList << list;
progress->setValue(progress->value() + 10);
qDebug() << "pro val = " << progress->value() + 10;
}
qDebug() << "All threads finished successfully - size" << finalList.size();
}
}
Dialog::~Dialog()
{
}
//this is not getting called as soon as I licked cancel
void Dialog::cancelAllThreads()
{
//here I want to cancel & clean up all the threads
qDebug() << "Canceled pressed....";
synch.setCancelOnWait(true);
}
To copy to clipboard, switch view to plain text mode
Now difficulties I am facing are:
1. GUI is not responding even if I click cancel. So cancelAllThreads() is not getting called (GUI thread is freezing some how),
this function is getting called when all the threads are finished even I cancel in middle.
2. I am not able to update progress bar values properly (As I don't have control over thirdPartyFunction, so while creating thread i am showing 50% &
when all the threads finished another 50% but this will give bad experience to the user as showing 50% at start and wait for long long time and then suddenly 100%).
3. Qt doc said I can not cancel QtConcurrent::run(..), is there a way to stop/kill this thread in middle
(doc said can not cancel but looking for some smart idea if possible) ?
What am I doing wrong here??
Bookmarks