PDA

View Full Version : connect and qthread



tom989
4th May 2013, 20:57
I'm trying to make a multithreaded pi calculation program.

It's working ok but my coding seems horribly messy (and no doubt 'wrong' in some sense).

I'd appreciate someone having a look and suggesting a better way of doing it.

The following gets executed upon a button press:



void MainWindow::findpifunc()
{


tabstack = new QVector<pi_display_box*>; //a vector containing a number of tabs (to view the pi calculation) // pi_display_box is a customised qtextedit
threadstack = new QVector <QThread*>; //a vector containing a number of threads
objstack = new QVector <Thr_object*>; //a vector of corresponding objects

for (int corestorun=0;corestorun<core_box->currentText().toUInt();corestorun++) //for the number of cores to run the calculation upon
{
tabstack->append(new pi_display_box); //add display widget to stack

threadstack->append(new QThread); //add a new qthread

objstack->append(new Thr_object); //add a new object

pi_bar->addTab(tabstack->at(corestorun),"Core " + QString::number(corestorun+1)); //opens new tab with display widget
}


for (int corestorun=0;corestorun<core_box->currentText().toUInt();corestorun++)
{
objstack->at(corestorun)->moveToThread(threadstack->at(corestorun)); //move the object to the relevant thread

connect (threadstack->at(corestorun),SIGNAL(started()),objstack->at(corestorun),SLOT(process())); //connect the started signal to process where the thread execution occurs

//when thread finishes
connect (objstack->at(corestorun),SIGNAL(finished()),threadstack->at(corestorun),SLOT(quit())); //deleting thread etc
connect (objstack->at(corestorun),SIGNAL(finished()),objstack->at(corestorun),SLOT(deleteLater()));
connect (threadstack->at(corestorun),SIGNAL(finished()),threadstack->at(corestorun),SLOT(deleteLater()));



objstack->at(corestorun)->settextdisplay(tabstack->at(corestorun),pidigits_box->currentText()); //a few arguments loaded into the object
threadstack->at(corestorun)->start(); //begin the code execution
}


}


Thanks!

amleto
5th May 2013, 23:56
how does each Thr_object know how not to repeat work already done?

Why are you using user input for core count instead of something like QThread::idealThreadCount() ?

Do you know you have memory leaks?