I was hoping I could get some advice on designing the proper threading for our project.
A little background info - our company does simulators of complex electrical/mechanical systems. These simulators could have fuel systems, electrical systems, pneumatic pressure systems, etc. I suppose you can just think of a car and that's close enough. We have done many of these simulators and have no problems with the systems themselves. However, we get requests from customers that have drastically different parts. Think of simulating one model of a car, including the ability to fail every individual component and system, then having to do a completely different model. For far too many years, we've rewritten our code base and had very poor code re-usability.
Currently, we are trying to make a centralized code base that can be used as our "core". We want to be able to switch from one model to another with minimal fuss and greatly improve our outdated design. Our method to this madness was to represent all objects as individual componenets derived from a similar base class. These components use an event driven model to pass data between each other dynamically, allowing a great number of systems to use this without actually having to know what the component is. We also have other base classes for appropriate systems, such as "IElectricalComponent". This mostly follows the Template design methodology:
With this, we should be able to build a new model by simply defining the specific parts of this model to the abstract factory, link up all the data messages, and let the systems run themselves.
The problem we're trying to overcome, however, is the sheer number of possible inputs. We get some of our data from a PLC using real switches and buttons to simulate the real thing, some of our data comes in from individual touch screens, some of it comes in from serial data streams from the real components, and some of it comes in from a control loader. We cannot guarantee until we get the contract how each simulator will behave. Because of this, we have very dynamic connections (and try our hardest to use UDP if possible). Our current model that we're replacing used a single thread for each connection, and did a lot of recreating code to define what that connection did. Because of our new model, we were trying to make this more scripted and dynamic.
The data mapping is not the problem, but the threading is. Since I cannot guarantee what is in each connection, I am uncertain where to put my semaphores/mutexes. Should each system (such as electrical) have it's own mutex, and each connection may have to lock several depending upon it's data? Should each connection have only one semaphore, which would require a giant global mutex lock on all systems while data is being set? Is a mutex generally fast enough that each component should have it's own data setting mutex and have hundreds of them possibly being set on a large chunk of data?
Any advice on how to thread this would be appreciated.