Wednesday, 15 June 2011

c++ - QTimer in multithreaded application -



c++ - QTimer in multithreaded application -

okay, i've lost in qtimer. problem is: have multithreaded application, , need work on qtimer's timeout. i've done this:

qtimer* timer = new qtimer(); timer->setsingleshot(true); connect(timer, signal(timeout()), someobject, slot(work()));

and did not work. sometimes, work() not called @ all, called when closed program, , seemed normal. i've come thought timer needs thread. provide mcv illustration :

class tester : public qobject { q_object public: tester(qobject* par = 0) : qobject(par) { } public slots: void greet() { qdebug()<<"hello"; } }; int main(int argc, char *argv[]) { qcoreapplication a(argc, argv); qtimer* timer1 = new qtimer(); qthread* thread = new qthread(); tester* tester = new tester(); timer1->setinterval(500); timer1->setsingleshot(false); timer1->movetothread(thread); qobject::connect(thread, signal(started()), timer1, slot(start())); qobject::connect(timer1, signal(timeout()), tester, slot(greet())); qobject::connect(timer1, signal(timeout()), timer1, slot(deletelater())); qobject::connect(timer1, signal(destroyed()), thread, slot(quit())); thread->start(); thread->wait(); delete thread; delete tester; homecoming a.exec(); }

and illustration nothing. not greet me, timeout not called, , not end, thread not stopped. questions are: 1. wrong code? 2. how utilize qtimer in multithreaded environment?

just calling qtimer::setinterval doesn't start qtimer. sets interval in qtimer::timeout signal emitted. didn't start qtimer. utilize qtimer::start.

i think you're doing several mistakes here. need move timer thread after connections made. not sure how safe start timer when thread starts because @ point, timer in 1 thread , thread object in thread. qtimer must started same thread in created. qthread object thread handler , lives in thread in created. see modifications made example:

#include <qcoreapplication> #include <qthread> #include <qdebug> #include <qtimer> #include <qobject> //#include "tester.h" class tester : public qobject { q_object public: tester(qobject* par = 0) : qobject(par) { } public slots: void greet() { qdebug()<<"hello"; } }; int main(int argc, char *argv[]) { qcoreapplication a(argc, argv); qtimer* timer1 = new qtimer(); qthread* thread = new qthread(); tester* tester = new tester(); timer1->setinterval(500); timer1->setsingleshot(false); timer1->start(); qobject::connect(timer1, signal(timeout()), tester, slot(greet())); timer1->movetothread(thread); thread->start(); homecoming a.exec(); }

it's not safest/best fix(memory leaks , other) is, hope, illustration on can build.

this in sentiment right , without memory leaks way utilize qtimer in qthread:

handler.h

#ifndef handler_h #define handler_h #include <qobject> class qtimer; class qthread; class tester; class handler : public qobject { q_object public: explicit handler(qobject *parent = 0); ~handler(); void exec(); private: qtimer* timer; qthread* thread; tester* tester; }; #endif // handler_h

handler.cpp

#include "handler.h" #include "tester.h" #include <qthread> #include <qdebug> #include <qtimer> #include <qobject> #include <qcoreapplication> handler::handler(qobject *parent) : qobject(parent) { timer = new qtimer; thread = new qthread(this); tester = new tester(this); timer->setinterval(500); timer->setsingleshot(false); qobject::connect(timer, signal(timeout()), tester, slot(greet())); qobject::connect(thread, signal(destroyed()), timer, slot(deletelater())); } handler::~handler() { thread->wait(); } void handler::exec() { timer->start(); timer->movetothread(thread); thread->start(); }

tester.h

#ifndef tester_h #define tester_h #include <qobject> class tester : public qobject { q_object public: tester(qobject* par = 0); public slots: void greet(); }; #endif // tester_h

tester.cpp

#include "tester.h" #include <qdebug> tester::tester(qobject *parent) : qobject(parent) { } void tester::greet() { qdebug()<<"hello"; }

main.cpp

#include <qcoreapplication> #include "handler.h" int main(int argc, char *argv[]) { qcoreapplication a(argc, argv); handler handler; handler.exec(); homecoming a.exec(); }

c++ multithreading qt qtimer

No comments:

Post a Comment