LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

What is the difference between using a global variable,passing a valuee and using a reference?

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

0 Kudos
Message 1 of 9
(3,937 Views)
If at all possible use wires.  If you have a lot of wires and it starts to look messy you can wrap them up into clusters.
PaulG.
Retired
0 Kudos
Message 2 of 9
(3,935 Views)

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

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 3 of 9
(3,929 Views)

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

Message 4 of 9
(3,923 Views)

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

0 Kudos
Message 5 of 9
(3,919 Views)

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. Smiley Wink

Message 6 of 9
(3,915 Views)

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

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 7 of 9
(3,914 Views)

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. Smiley Wink


OK, OK they aren't evil.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 8 of 9
(3,908 Views)

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

Message 9 of 9
(3,880 Views)