The program has unexpectedly finished.
Now, I was able to break the loop by setting isAlive to false, and to terminate the thread properly.
The problem came, even if I used
QThread clockThread;
TrafficClock trafficClock;
trafficClock.threadSetup(clockThread);
trafficClock.moveToThread(&clockThread);
clockThread.start();
ClockThread implementation:
for(int iii=0;iii<100000;iii++)
{
if(continueTick==true)
{
emit stepTimeSignal();
}
QThread::msleep(333);
}
When trying to kill the thread, the thread just continued:
clockThread.terminate();
while(!clockThread.isFinished()){}This came even if I used quit() and exit().
After some investigations, I found that the thread wasn't terminated until the work in the thread was done. With this information, I was able to kill the thread by using a local variable isAlive:
while(isActive==true)
{
for(int iii=0;iii<100000;iii++)
{
if(continueTick==true)
{
emit stepTimeSignal();
}
}
QThread::msleep(333);
}
Now, I was able to break the loop by setting isAlive to false, and to terminate the thread properly.
Why all the fuss about a thread still running after closing the program? As this program gets more complex, so will the bugs. Small glitches in the code will give me complex problems in the future, and I need to avoid it as soon as possible.
Also:
There are disagreements in the Qt community about how to implement threads.
- Subclass the worker klass from Thread, create a object with new for the thread and use the start() method to start the thread. This approach is recommended by the official QT documentation
- Instead, I create a normal object and I move it to a thread.
You can follow the discussion below
No comments:
Post a Comment