As suggested in this question, I am now trying to incorporate multithreading.
According to the links given by karlphillip, I understand that the documentation about subclassing QThread is not to be followed and to use moveToThread()
as explained. Now I see that default implementation of QThread run()
has only an exec()
which must then be ended by calling quit()
when worker thread has finished operations. I have a few questions now so that I understand things better:
QApplication* ptrApp=new QApplication(argc,argv);
QThread* th=new QThread;
MyClass* obj=new MyClass;
obj->moveToThread(th);
QObject::connect(th,SIGNAL(started()),obj,SLOT(someFunct()));
QObject::connect(obj,SIGNAL(over()),th,SLOT(quit()));
th->start();
//some GUI code in main thread here
return ptrApp->exec();
What happens if I continue to use
someFunct()
even after I emitover()
from withinsomeFunct()
? Is it undefined behaviour or normal?Which thread would
obj
now be associated with (while the rest of the code after emittingover()
is still executing insomeFunct
)? My understanding is: it cannot be inth
when I havequit()
that thread...quit()
will be queued until theexec()
in the main thread executes it which will cause theexec()
inrun()
ofth
to exit (I hope I am not making a mistake here). I assume that thread is no longer existent.Once slot
quit()
forth
is executed, is it safe to assume that the thread has indeed quit or should I further connectfinished()
signal ofth
to some slot to be absolutely sure?