Wednesday 16 July 2014

TrafficControl: Thread running after closing window [Solved]

When I introduced threads to the program, I also introduced a problem: the thread didn't terminate when I closed the User Interface. Instead, it carried on after calling the terminate() method and I saw that the thread was still active in the console output, after closing the main program. When closing the console application manually, the console created an error message:

The program has unexpectedly finished.

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




Sunday 13 July 2014

TrafficControl: Data Structures of the program

In future posts, I'll discuss errors and improvements in my program. To make this easier to understand, I'll describe the overall structure of the program.

TrafficControl contains list of all trains, stations and tracks. The TrafficClock is associated with a thread that fires on a periodic interval. When the TrafficClock emits the signal, all train/track/station elements are updated.

The datamodels are used for presenting the current state of the elements of the train network.

Tuesday 1 July 2014

TrafficControl: The First Demonstration of TrafficControl

After implementing a thread model, I can show a screen cap video of the program.



3x normal speed. All units are SI. The stops are very short. A fix is coming...

All numbers are in SI units. The train starts at Hjärup, travels to Lund C and stops there. After that, it travels to Gunnesbo, back to Lund C and to Stångby.

The train network isn't hard coded, instead it is specified in an external file that defines the trains, stations, tracks and travel plans:


The program is still in an early phase, but the functionality is constantly growing. If you have any suggestions, ideas or issues that need to be addressed, please comment.

Adding more trains is easy, and results in three trains moving independently:
Future versions of the program will prevent train collisions and congestion.

I have a list of work packages/incident to deal with:


Hedy Lamarr was a Hollywood actress and inventor of the CDMA technology (leading to Wi-Fi). Her story deserves a blog post of her own, and I will try not to fail her.