LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

RE: local variable cross-talk?

Jeff

A global can be considered as a variable to the entire code, where lots of
different vi's can operate it. Locals only have any meaning within their
own vi, or instances of their own vi.
If anything writes to a particular global, whether in reenterent vi's or
not, it's available everywhere in the code.

What you were asking was about locals though. If a vi is non-renentrant
(i.e. as they come out of the box) then the values in the locals are vi
specific, no matter where the vi is, where it was last used, it retains the
data from it's last operation. If it is re-entrant, then the values in the
locals for each occurance of the vi are its own for EACH instance, i.e. just
like different locals in different vi's, it doesn't matter where else the vi
is used, the data held is that from the last operation of that specific
instance.

Simple way to demonstrate this. Make a vi that has one numeric control,
then code in +1 and get it to write to a local variable for that control.
Throw in an indicator to wire out the result of what is written to the local
for the control.
Then take this vi, put it in a for loop, then put another copy in the for
loop as well. Wire the indicators to the side of the for loop and create
indicator arrays for them. Get the loop to run 6 times say. Now try
running this with the vi in the for loop non-reentrant and then reenterrant.

One way the arrays will contain either all the odd no.s 1,3,5,7,9,11 and
then the other array 2,4,6,8,10,12 (don't know which array will be which,
depends which executes first in the for loop of this example). The other
way they will both be 1,2,3,4,5,6. In the first example, the same vi has
run 12 times, i.e. one copy of the vi which retains its info and is called
in many places and therefore only has one set of values, and the other with
two re-enterant copies where they have their information specific to that
instance of them i.e. effectively they are different vi's. Both ways are
useful, depends what you want.


If you're looking for a use for non-reenterant vi's then consider this:-

For instance. Supposing you get many things to try to write to a global
array of numbers, and you have two vi's one "A" writes to the first element
in the array and the other "B" writes to the second element. Because in LV
you have to read a global first and then write to it to perform a change,
these independant vi's "A" and "B" (be they re-enterant versions of the same
vi, or different vi's), can be performing the tasks simultaneously in the
code. I.e. "A" reads, "B" reads, "A" writes, and "B" writes over the top
with a modified version of what it read, and "A"'s changes are lost. This
is what's known as a "race condition" as "A" hadn't finished and "B" needed
to know what "A" was going to write before "B" performed "A" read. Try it,
hours of fun if you code this kind of thing in inadvertantly!

If the same vi is used, and is not re enterant, it can only run in one
instance at a time, hence two read / write operations cannot be performed
together, problem solved. Unless that gives you timing issues of course,
waiting for one to finish, to write the other......but that's another whole
can of worms.

cheers
Tim Price

This e-mail, its content and any files transmitted with it are intended
solely for the addressee(s) and are confidential and may be legally
privileged. Access by any other party is unauthorised without the express
prior written permission of the sender. If you have received this e-mail in
error you may not copy, disclose to any third party or use the contents,
attachments or information in any way.








-----Original Message-----
From: comp.lang.labview@omigatech.co.uk
[mailto:comp.lang.labview@omigatech.co.uk]On Behalf Of Jeffrey W Percival
Sent: 29 November 2001 21:12
To: comp.lang.labview@omigatech.co.uk
Subject: Re: local variable cross-talk?



Another useful reply! What a great news group this is.
One last thing I wanted to ask about, though, is global vs. local. I see
you talk about globals, but in fact the variables in my subVI's were locals.
I can easily understand the behavior I observed accompanying the use of
global variables, But I guess the use of the word "local" stumped me.

Should I interpret "local" in LabVIEW's sense to mean "local to all
instances of this VI"? And global to mean "visible to all instances of this
VI as well as other VI's"?

-Jeff


Tim Price wrote:

This facility is actually very useful, for instances where you want to
encapsulate some code so that it can only run in one place at a time,
i.e.
global arrays that are written to in more than one place. This method
can
eradicate race conditions completely for example when used like this.
There
are multiple other uses as well.
However, using a vi as a module of code, to run in more than one
instance at
a time simultaneously, re-entrant is the way to go. Just make sure you
debug it first!!!
Remember though, just because a vi may be re-eneterant, doesn't mean
that
everything inside is; sub-vi's, Globals etc. The classic one I've seen
is
where people think that a re-enterant vi is talking to it's own copy of
any
Globals used within it, i.e. counters etc., where in actual fact of
course,
they are all using the same Globals.

Worth playing with a few examples to get familiar with it.

Tim Price

--
---
Jeffrey W Percival, Senior Scientist and Associate Director
Space Astronomy Laboratory, University of Wisconsin - Madison
1150 University Ave, Madison, WI 53706 USA
608-262-8686 (fax 608-263-0361) jwp@sal.wisc.edu
http://www.sal.wisc.edu/~jwp
0 Kudos
Message 1 of 2
(2,624 Views)
Tim Price wrote:

Tim, thanks very much. I'll try the experiment you suggest.

Thanks for taking the time.

-Jeff

--
---
Jeffrey W Percival, Senior Scientist and Associate Director
Space Astronomy Laboratory, University of Wisconsin - Madison
1150 University Ave, Madison, WI 53706 USA
608-262-8686 (fax 608-263-0361) jwp@sal.wisc.edu http://www.sal.wisc.edu/~jwp
0 Kudos
Message 2 of 2
(2,624 Views)