PDA

View Full Version : question about threads: calling 'start()->"



ErrMania
22nd November 2011, 23:53
Hi,

i was wondering something about threads :

if you initalize your class, and then you call its instance with "start()", won't this class be called two times, once for the instance, and the second time for the thread pool? (whereas we just want to call the class once, on a separate thread)

example :


void MyClient::readyRead(){
MyTask *mytask = new MyTask();
…
QThreadPool::globalInstance()->start(mytask);
}

Thanks

qlands
23rd November 2011, 13:53
Hi,

Why you just not use QThread. For example:



class myBigProcess : public QThread
{
Q_OBJECT
public:
explicit myBigProcess(QObject *parent = 0);

void run();
};





myBigProcess::myBigProcess(QObject *parent) :
QThread(parent)
{

}
void myBigProcess::run()
{
///Massive process here
}


Then you can do:



myBigProcess *MBP;

MBP = new myBigProcess(this);
MBP->start(); //This will call run();


QThread has signals to know when the run() finished.

Carlos.

Lesiok
23rd November 2011, 16:34
QThreadPool::start starts a run method on the passed object and initializes it does not.

ErrMania
23rd November 2011, 18:36
oh okay, thanks for your answers