Hi together,
I have some problems with understanding the behaviour of my following code...
I tryed to udnerstand the Threading in Qt, so i found a nice youtube vid which i would like to share with you - its from Dario Freddi http://www.youtube.com/watch?v=MMFhc2jXzgw
I Wrote a simple application to try movetothread methode, just 2 Workers and a QWidget class with a button. on button clicked worker 1 starts -> in its cTor it creates worker2 and waits of the finish signal from worker2. But some code say more than words so.
i can post the whole simple project, but i dont know if its ok, so i just post the some few lines of code.
QWidget:
void Widget::on_btTest_clicked()
{
time.start();
worker1 *worker = new worker1;
worker->doWork1();
qDebug() << time.elapsed() << "mseconds" << "current time:" << time.currentTime().toString("hh:mm:ss:z")<< "worker1 result" << worker->getRes();
}
void Widget::on_btTest_clicked()
{
QTime time;
time.start();
worker1 *worker = new worker1;
worker->doWork1();
qDebug() << time.elapsed() << "mseconds" << "current time:" << time.currentTime().toString("hh:mm:ss:z")<< "worker1 result" << worker->getRes();
}
To copy to clipboard, switch view to plain text mode
Worker1:
worker1
::worker1(QObject *parent
) : m_resultString("Hallo")
{
moveToThread(m_classThread);
m_worker2 = new Worker2;
//breaks the own event loop if worker 2 is finished
connect(m_worker2, SIGNAL(workDone()), &wait, SLOT(quit()));
connect(this, SIGNAL(startNow()), m_worker2, SLOT(doWork2()));
m_classThread->start();
}
void worker1::doWork1()
{
//starts worker2 und wartet auf sein finished
qDebug() << Q_FUNC_INFO;
emit startNow();
wait.exec();
}
{
return m_resultString;
}
worker1::worker1(QObject *parent) :
QObject(parent),
m_classThread(new QThread),
m_resultString("Hallo")
{
moveToThread(m_classThread);
m_worker2 = new Worker2;
//breaks the own event loop if worker 2 is finished
connect(m_worker2, SIGNAL(workDone()), &wait, SLOT(quit()));
connect(this, SIGNAL(startNow()), m_worker2, SLOT(doWork2()));
m_classThread->start();
}
void worker1::doWork1()
{
//starts worker2 und wartet auf sein finished
qDebug() << Q_FUNC_INFO;
emit startNow();
wait.exec();
}
QString worker1::getRes()
{
return m_resultString;
}
To copy to clipboard, switch view to plain text mode
Worker2:
Worker2
::Worker2(QObject *parent
) : m_resultString("123")
{
moveToThread(m_classThread);
tima.setInterval(5000);
tima.start();
m_classThread->start();
}
void Worker2::doWork2()
{
qDebug() << Q_FUNC_INFO;
// create a event loop for the timer to be finished
connect(&tima, SIGNAL(timeout()), &wait, SLOT(quit()));
wait.exec();
emit workDone();
}
Worker2::Worker2(QObject *parent) :
QObject(parent),
m_classThread(new QThread),
m_resultString("123")
{
moveToThread(m_classThread);
tima.setInterval(5000);
tima.start();
m_classThread->start();
}
void Worker2::doWork2()
{
qDebug() << Q_FUNC_INFO;
// create a event loop for the timer to be finished
QEventLoop wait;
connect(&tima, SIGNAL(timeout()), &wait, SLOT(quit()));
wait.exec();
emit workDone();
}
To copy to clipboard, switch view to plain text mode
Results in the Debug frame:
void worker1::doWork1()
void Worker2::doWork2()
void worker1::doWork1()
void Worker2::doWork2()
5003 mseconds current time: "15:35:22:36" WIDGET: worker one "Hallo"
7204 mseconds current time: "15:35:22:36" WIDGET: worker one "Hallo"
void worker1::doWork1()
void Worker2::doWork2()
void worker1::doWork1()
void Worker2::doWork2()
5003 mseconds current time: "15:35:22:36" WIDGET: worker one "Hallo"
7204 mseconds current time: "15:35:22:36" WIDGET: worker one "Hallo"
To copy to clipboard, switch view to plain text mode
So here is my missunderstood/question -> if i click the button twice with a simple delay of 2 seconds, why get i the results at the same time? Why i dont get the debug msg of the first click on the button from(worker.getRes() (in the widget class) not 2 seconds earlier than the msg of the 2nd click?
If I may suggest, then I would argue that the problem is that the queue for the widget/main thread queue is accessed when the 2nd click -> workers are finished? So the first debug msg got queued at the mainthread and fired when the last work is done? But i dont get it exactly whats the problem here.
It would be very nice if s.o can explain me the above behaviour. An it would be really nice , to see how to solve this "problem". thanks!
(if u want me to post the project as attachement - just tell me....)
TIA Anenja!
Bookmarks