Hello,
I have walked into a project of pre-existing code that I must enhance, repair, etc.
This project uses 3 threads, and each thread "owns" a particular piece of hardware, for example:
Thread 1: owns DC power supply
Thread 2: owns waveform generator
Thread 3: owns NI multifunction I/O DAQ card
I am using the term "own" to indicate that all functions that perform actions on that hardware are performed within that thread (for example, the source code of Thread 1 has functions like InitPowerSupply, ProgramPowerSupply, PowerSupplyOff, etc.).
I'm not sure I like the architecture, but it's what I've walked into and so I am trying to live with it.
One question I have is: how to access functions that reside in one thread while running in a different thread? For example, say I'm in Thread 2 and I want to turn off the power supply by calling PowerSupplyOff(); What is the best way to do this? I could declare the PowerSupplyOff() function as an extern in Thread 2 and just call it directly--in this case, does the compiler make a separate copy of this function, one copy for each thread that calls it, or is it one piece of code that is shared between threads (if the latter is true, what are my synchronization options to avoid competition between calling threads?)? Another option would be to use SetEvent to signal Thread 1 that Thread 2 wants to call PowerSupplyOff--then, in Thread 1 source code, there could be an event case to simply call PowerSupplyOff()--this option localizes all hardware calls within the thread that owns that particular piece of hardware--but is that necessary?
Long question, but to summarize: in Thread 2 is it OK to simply declare the power supply functions of Thread 1 as externs and call them directly?
If anybody has any thread architecture philosophies, too, I would be very interested in hearing your opinions.
Thank you!
-SciAuto