c++ - Can I use QTimer to implement a multthreaded algorithm? -



c++ - Can I use QTimer to implement a multthreaded algorithm? -

currently need implement multi-threaded algorithm based on qt. maybe should seek extend qthread. before that, i'd ask, if can utilize 2 qtimers timer1, timer2, , connect timeout signal threads respectively, implement "fake" multi-threaded program?

you can connect timeout() signal of qtimer appropriate slots, , phone call start(). on, timers emit timeout() signals @ constant intervals. 2 timers run on main thread , in main event loop. can not phone call multithreaded. because 2 slots not run simultaneously. run 1 after other , if 1 blocks main thread, other never called.

you can have real multithreaded application having classes different tasks should done concurrently , utilize qobject::movetothread alter thread affinity object :

qthread *thread1 = new qthread(); qthread *thread2 = new qthread(); task1 *task1 = new task1(); task2 *task2 = new task2(); task1->movetothread(thread1); task2->movetothread(thread2); connect( thread1, signal(started()), task1, slot(dowork()) ); connect( task1, signal(workfinished()), thread1, slot(quit()) ); connect( thread2, signal(started()), task2, slot(dowork()) ); connect( task2, signal(workfinished()), thread2, slot(quit()) ); //automatically delete thread , task object when work done: connect( thread1, signal(finished()), task1, slot(deletelater()) ); connect( thread1, signal(finished()), thread1, slot(deletelater()) ); connect( thread2, signal(finished()), task2, slot(deletelater()) ); connect( thread2, signal(finished()), thread2, slot(deletelater()) ); thread1->start(); thread2->start();

note task1 , task2 inherit qobject.

c++ multithreading qt qthread qtimer

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -