c++ - Parallel threads, QT -


i'm newbie @ qt , i've not lot of expirience working threads. i've readed many guides threads , found there @ least 3 ways work threds. i'm interested in 1 of them, there example: i've made class , i'm trying use function in thread.

.h

#ifndef identificator_h_ #define identificator_h_ #include <qtcore> #include <qcoreapplication>  class identificator:public qobject { q_object public:     identificator(int i);     virtual ~identificator(); private: int id; private slots: void printid(); public: void setid(int i); int getid(); signals: void finished(); }; 

.cpp

#include "identificator.h" #include <stdio.h> #include <qdebug> identificator::identificator(int i) { id=i; }  identificator::~identificator() {     // todo auto-generated destructor stub } void identificator::setid(int i) { id=i; } int identificator::getid() { return id; } void identificator::printid() {     for(int i=0;i<10;i++)     { qdebug()<<" "<<this->getid()<<" "<<i<<" "<<this->thread();     }emit finished();  } 

and main.cpp

int main(int argc, char *argv[]) {     qcoreapplication a(argc, argv);     printf("hello\n");     identificator *first=new identificator(0);     identificator *second=new identificator(0);     qthread *thread1=new qthread;     qthread *thread2=new qthread;     first->setid(3);     second->setid(4);     first->movetothread(thread1);     second->movetothread(thread2);     qobject::connect(thread1, signal(started()), first, slot(printid()));     qobject::connect(thread2, signal(started()), second, slot(printid()));     qobject::connect(first, signal(finished()), thread1, slot(quit()));     qobject::connect(second, signal(finished()), thread2, slot(quit()));     thread1->start();     thread2->start();     return a.exec(); } 

i thought, in console output there like: 3 4 3 3 3 4 4 have 4 4 4 4 4 4

i'm need in help, thanks!



i have next output:

hello 4 0 qthread(0x8053268) 3 0 qthread(0x8053250) 3 1 qthread(0x8053250) 3 2 qthread(0x8053250) 3 3 qthread(0x8053250) 3 4 qthread(0x8053250) 3 5 qthread(0x8053250) 3 6 qthread(0x8053250) 3 7 qthread(0x8053250) 3 8 qthread(0x8053250) 3 9 qthread(0x8053250) 4 1 qthread(0x8053268) 4 2 qthread(0x8053268) 4 3 qthread(0x8053268) 4 4 qthread(0x8053268) 4 5 qthread(0x8053268) 4 6 qthread(0x8053268) 4 7 qthread(0x8053268) 4 8 qthread(0x8053268) 4 9 qthread(0x8053268) 

your output correct, , program works correctly

  • thread1 start , preempted before printing anything
  • thread2 start, output 4, interrupted
  • thread1 has processor, output 3 ten times, till emit finished
  • thread2 has processor again , can output 4 9 times, till emit finished

couple of things:

  • the number of iteration in printid small see real preemption between both threads (even if in case t2 interrupted once)
  • qdebug doesn't flush output. put newline @ end. means miss output lines on console. totally possible see 1 value printed.
  • you cannot expect specific output code. permutation of ten 3 , ten 4 may occur.
  • it seems qdebug has lock, otherwise see mangled output because don't flush.

try same exercise more 10 iterations, no flushing, no output sync

std::cout <<" "<<this->getid()<<" "<<i<<" "<<this->thread() << "\n"; 

and flushing (no need sync)

std::cout <<" "<<this->getid()<<" "<<i<<" "<<this->thread() << std::endl; 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -