01-16-2009 01:08 PM
I have created a project that consists of several VIs. Only the main VI has a front panel and the others perform functions. The function VIs are dependent on controls on the main VI's front panel. I have several ways of passing the value of the controls. One is to use a global variable and just place it on the dependent VIs. Another option is to strictly connect the terminal from the control to a VI connector block and pass the value directly. My last option is to create a reference of the control and reference it inside the dependent VIs, but this would also require connections to be made to the VI block.
What are the advantages/disadvantages of these options?
-Stephen
01-16-2009 01:12 PM
01-16-2009 01:18 PM
5thGen wrote:I have created a project that consists of several VIs. Only the main VI has a front panel and the others perform functions. The function VIs are dependent on controls on the main VI's front panel. I have several ways of passing the value of the controls.
1) One is to use a global variable and just place it on the dependent VIs.
2) Another option is to strictly connect the terminal from the control to a VI connector block and pass the value directly.
3) My last option is to create a reference of the control and reference it inside the dependent VIs, but this would also require connections to be made to the VI block.
What are the advantages/disadvantages of these options?
-Stephen
1) Globals are evil and introduce race conditions.
2) The sub-VI only get the value when it was called and updates that occur while the sub-VI is runing are not sensed by the sub-VI
3) This uses property node "value" or "value signaling" both of which run the user interface thread which is single-threaded and you incur a thread swap hit to performance. You also have a potential for race conditions.
The are various methods for sharing dat to/from sub-VI which include Queues and Action Engines.
I hope that hleps,
Ben
01-16-2009 01:27 PM
Well said Ben,
@Ben wrote:
2) The sub-VI only get the value when it was called and updates that occur while the sub-VI is runing are not sensed by the sub-VI
If you call a sub-vi and wait until it returns before proceeding with execution, then this is a non-issue. But if there is a parallel thread where the sub-vi is called and you want the sub-vi to have the latest value while it is running, then the above is definitely true. It depends on the intended behavior of the sub-vi.
R
01-16-2009 01:33 PM
Thank you for the responses. If I have this correct, if I have a subVI that is running continuously and it needs the updated value during its cycles, then a hard-wire of the value will not work. But, if instead of hard-wiring the value and instead use a reference, will this give the subVI to the updated value?
-Stephen
01-16-2009 01:35 PM
Ben wrote:1) Globals are evil and introduce race conditions.
I'll agree to the second part, but with a minor addendum to the second: can introduce race conditions (if used improperly). I use global variables and I don't have race conditions.
I will not agree to the first part.
01-16-2009 01:39 PM
5thGen wrote:.... But, if instead of hard-wiring the value and instead use a reference, will this give the subVI to the updated value?
-Stephen
A property node >>> Value will return the value associated with the control with which the reference is associated at the time the proprty node executes. So if you read it once you will get one update, read it in a loop, you will get the current value each time the node executes.
If your sub-VI needs to determine when the value changes, a dynamic event in the sub-VI could be set-up to fire when the value changes.
Ben
01-16-2009 01:44 PM
smercurio_fc wrote:
Ben wrote:1) Globals are evil and introduce race conditions.
I'll agree to the second part, but with a minor addendum to the second: can introduce race conditions (if used improperly). I use global variables and I don't have race conditions.
I will not agree to the first part.
![]()
OK, OK they aren't evil.
Ben
01-16-2009 02:56 PM
Ben wrote:OK, OK they aren't evil.
Ben
We almost saw the dawn of Ben's new thread called "Evil Globals"..or "Global Abuse".. 😉
Back to topic..
Again it depends what behavior you want within your sub-vi. If you want it to run & do something everytime a value changes, then Ben's second suggestion is good by calling the sub-vi and pass the new value. However, if the sub-vi is busy doing other things and needs the latest value to do "its thing", then using a reference or an ActionEngine might be the way to go. Again, depending on what the sub-vi does, you might also consider a DynamicVI (daemon) that is spawned, executes independently (does "its thing") and obtains or gives values as the value changes. This is where ActionEngines are useful.
It all depends on the desired behavior or the sub-vi....
R