Pitfalls of Qt Signals and Slots in embedded real-time systems

July 27, 2012

Qt Signals and slots can be very useful. Together with a model-view-controller framework, where the model emits signals to views and controllers, a very clean and logical system can unfold. However, one must be aware of a couple of pitfalls.

First, there is no control over the order in which slots are called from signals. This is true in both Queued connections and Direct connections. In complex systems, with many developers, there is a tendency for control to proliferate, and then multiple controllers are watching a signal. Then dependencies can develop between the outputs of these various controllers, sometimes with dire consequences.

The second problem is that once this disconnected architecture is in place, there is a tendency to write multiple signal observers without regard to other observers. So a developer may fire a signal, unaware that two other developers have slots watching those signals, and those slots may consume a lot of computational resources. For example one slot may drive a UI element to be created, while another slots starts a real-time process. So you may fire your signal expecting your slot to execute right away, and become bewildered when your slot code does not fire for 200 ms or longer.

There are no execution flow controls built-in to the Qt signal slot system, so the system architects must keep tabs on the signal flow. I find that its best to separate the model, the controllers, and the viewers into separate threads, then develop an asynchronous closed loop communication and synchronization scheme on all channels (with a managed memory scheme). When intentionally designing this type of communication between asynchronous elements, one is forced to wait for events to complete, and design with the expectation that execution times vary. Events that cannot be separated by an asynchronous gaps in time are collected together into a single thread context to gain determinism. This makes for a more robust design.

Entry filed under: Qt. Tags: .

Deleting Qt Threads without crashing David’s Principles of Software Development