QThread and loop
-
Using a QThread and a worker (not subclassed from QThread, but subclassed from QObject and is using thread event loop with signal/slots) I have a "permanent" loop to execute in a thread. In the old days of pthread, you might have:
void mythread() {while (!terminated) {do stuff......}}
Where terminated might be a global var, or in a non-global scope somewhere. Now, for Qt...how to tell if the thread is terminated by an outside thread or the main? I want the worker function to terminate nicely!
The worker threaded function will be moved to the QThread's thread. So, for best practice should I:
- use QThread's "isRunning/isFinished"?
- check the actual thread state?
- use signal/slot to trigger a state variable private to my worker?
Also, if I use a signal/slot to fire off the "eternal" function (ie it doesn't return unless thread terminated)...does this block the thread's event loop?? Thanks!
-
Even in a thread, the thread's eventloop needs to be allowed to run. If your thread code is in a tight loop (without a call to processEvents() at a very minimum,) then your loop will block the thread's eventloop.