BreakPoint

cancel
Showing results for 
Search instead for 
Did you mean: 

Why some people say Local Variables are bad...

Oh dear...and why not use the abort button to stop?

 

 

 

Message Edited by for(imstuck) on 06-15-2010 12:43 PM
0 Kudos
Message 211 of 396
(8,455 Views)
Unless he's got another loop in the code that changes the value of Logging 2 to False, that loop's gonna run for a long time..
0 Kudos
Message 212 of 396
(8,413 Views)

Well, i'd say since it is a local variable, the user could press that button.....

But the outer loop cannot be stopped except by ABORT.....which reminds my of the famous tree.

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 213 of 396
(8,402 Views)
Maybe it's hiding under that green rectiangle labeled 1Smiley Very Happy
Regards
Ray Farmer
Message 214 of 396
(8,381 Views)

Here's another one.

 

0 Kudos
Message 215 of 396
(8,224 Views)
0 Kudos
Message 216 of 396
(8,091 Views)

Here's an excellent post / explanation from Norbert.

 

It is found here:  http://forums.ni.com/t5/LabVIEW/Having-Trouble-Copy-and-Pasteing-Local-Variable-in-LabVIEW-2010/m-p/...

 


@Norbert B wrote:

I think it is a good time to tell him the reason for locals being "bad":

Local variables are (using terms of other programming languages) "call by reference" only for writing. Reading is done "by value" so each reading local variable represents a copy of the value from the control the local is linked to.

Taking into account that the local variable node does not provide an error in/out for "synchronization" purpose, the read is done any time the dataflow would allow it. This "random moment in time" for reading is commonly the reason for race conditions: you read old or new data but you never know for sure.

 

This is the reason for using variables (globals work the same!) should be avoided. But as different persons already posted: variables have their use and therefore cannot be removed from LV. But please do not consider variables to be botch! They simply work different compared to other programming languages.

 

Regarding performance:

Using the terminal/wire to transfer data is using X. Variables introduce a slight overhead. Sadly, this overhead depends on the system you are running on and on the LV version.

Just to give you some figures: I use LV 2010 and the local variable is about 10% slower than writing to the terminal for update of the indicator.

The value-property node is about 1000 times slower than the terminal due to the switching of threads (as being posted before). You can "cheat" a bit by hiding/minimizing the frontpanel, but it'd be still about 900 times slower.

This is the reason why the VI Analyzer suggests you to exchange all value-property nodes with local variables. This can be done when:

a) error in/out is not used for synchronization

b) performance matters

 

So please make it a rule of thumb:

- Try to avoid any type of "remote access to controls/indicators". Those mechanisms are local/global variables and value-property nodes. The latter ones lack of performance due to heavy thread swaps and all of them easily create race conditions if not used properly.

- There are situations where variables come handy and can be used. Those are most of Write Once, Read Many. But you should read the info tbob linked in his "signature" because this is an elegant way to prevent using variables.

 

hope this helps,

Norbert

 

PS: Without seeing the issue you described in your very original post, we cannot give you any valuable information about the issue. So please post code where we can reproduce this. Otherwise, you will never get any answer!


Message 217 of 396
(8,030 Views)

@Ray.R wrote:

Here's an excellent post / explanation from Norbert.

 

It is found here:  http://forums.ni.com/t5/LabVIEW/Having-Trouble-Copy-and-Pasteing-Local-Variable-in-LabVIEW-2010/m-p/...

 


@Norbert B wrote:

Taking into account that the local variable node does not provide an error in/out for "synchronization" purpose, the read is done any time the dataflow would allow it. This "random moment in time" for reading is commonly the reason for race conditions: you read old or new data but you never know for sure.



Should we propose error in/out for local variables for synchronisation purposes?  Would this make Locals less evil?

0 Kudos
Message 218 of 396
(8,013 Views)

I was thinking about that this morning.

 

Then you would have people not wiring the error-in/out...

 

Maybe simply change their name so that people not confuse them with text-based "Local" variables.

Humm..... in essence, they are similar, in that it is local to the VI or function...  But it is more of a copied instance of the data than the variable itself.. 

If it wasn't for the dataflow and the parallelism in which LabVIEW executes, then they would be fine...

 

I don't know what the solution is... And I hate seeing the communitu divided by a Local Variable..  😞

0 Kudos
Message 219 of 396
(7,990 Views)

@Intaris wrote:

@Ray.R wrote:

Here's an excellent post / explanation from Norbert.

 

It is found here:  http://forums.ni.com/t5/LabVIEW/Having-Trouble-Copy-and-Pasteing-Local-Variable-in-LabVIEW-2010/m-p/...

 


@Norbert B wrote:

 

Taking into account that the local variable node does not provide an error in/out for "synchronization" purpose, the read is done any time the dataflow would allow it. This "random moment in time" for reading is commonly the reason for race conditions: you read old or new data but you never know for sure.

 



Should we propose error in/out for local variables for synchronisation purposes?  Would this make Locals less evil?


You would have to re-invent the local such that it was protected by a semaphore so we could do a "Locked-Read" proprty taht would return the value along with a semaphore locking reads of the local until that thread wrote back to the local and released the semaphore.

 

So whole idea;

 

Only one local write allowed.

All others must be a protected write.

 

Seems silly,

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 220 of 396
(7,986 Views)