LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Why not global variables in LabVIEW

I have used globals to pass data for years without any problems. I do not believe they should be used without thought, but do beleive that they can be used without problems. References seem to me to get complex fast for accessing data in arrays of clusters. Globals make accessing the information easy. Also do you really run into problems when reading and writing a global at the same time. This has worked well for me in the past. Is there something in 6i or 6.1 that may change this? Also if globals and local variables are so bad, then why does NI produce so many examples that use them? I'm a strong advocate of modular code, but this all or none view of globals has me perplexed.
0 Kudos
Message 1 of 7
(5,223 Views)
There is no reason not to use them but the very fact that they are Global makes it risky. Nothing is saying that you cannot use them, just be sure you are not reading to the same global at the same time or reading from the variable before it's been written to,etc...

Again there is no reason not to use them just be careful. References can be somewhat more complicated but when used are very effective. There has been no change from 6i to 6.1, globals are globals as they are in any language.
0 Kudos
Message 2 of 7
(5,222 Views)
Hi Fair...,

As stated elsewhere, globals can be dangerous for any number of reasons.

I do not use globals.

I work with globals used by others.

Globals do not have a diagram. With no diagram, I cannot control the behaviour.

On the the other hand, LV2 globals do have a diagram.

I can control exactly what piece of data is being manipulated at any one time by putting the update code riht in the LV2 global. Provided the LV2 is set as non-reentrant, I never have to worry about one thread updating something while another filed is being updated.

I like diagrams.

Ben
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 3 of 7
(5,222 Views)
"fairhairedwineboy" wrote in message
news:506500000008000000BC4F0000-1023576873000@exchange.ni.com...
> I have used globals to pass data for years without any problems. I do
> not believe they should be used without thought, but do beleive that
> they can be used without problems. References seem to me to get
> complex fast for accessing data in arrays of clusters. Globals make
> accessing the information easy. Also do you really run into problems
> when reading and writing a global at the same time.

Atomicity: Hopefully somebody from NI can jump in and write a full manual
about atomicity, because it is CONFUSING in labview. Search for atomic on
ni.com and get scientific results, or atomicity and get zero results;
useless!!

However from the f
orum and my experience, unless re-entrancy is selected
then items occur atomically within a thread. Therefore I would suspect
reading/writing globals also occurs atomically but I really have never seen
this documented... let's see if NI jumps in with what happens there.

Difficulty: Ok so let's say you have a global and want multiple threads to
read/write properly, but want to make no assumptions? Or more likely you
have some other shared resource. Consider a lock! You can use their
semaphores but those don't atomically test-and-aquire. You'll probably need
to make sure that if re-entrantcy is unchecked your VI will be executed
atomically.

However this is tough. That might be why globals are generally discouraged.
Just like in any programming language, globals deal with shared resources
which the average lackey does not understand.

Not that you need the help, but for anybody else reading the forum consider
reading the standard college text: Modern Operating Systems by Tannenbaum.
It
describes the problem and solutions. That or pay me to solve your
problems! 😉

-joey
0 Kudos
Message 4 of 7
(5,222 Views)
> Atomicity: Hopefully somebody from NI can jump in and write a full manual
> about atomicity, because it is CONFUSING in labview. Search for atomic on
> ni.com and get scientific results, or atomicity and get zero results;
> useless!!
>


The reason you won't find it mentioned is that you don't generally need
to worry about it on the diagram. All globals and locals property
nodes, controls, etc. are written and read atomically. The dataflow
diagram takes care of most storage access, and for things like globals
where it doesn't, the access is protected. The only time you would need
to be concerned with an atomic access is if you were inside of a DLL,
CIN, or driver that you were adding into LV. What you do have to be
concerned about is the next level up, which we generally refer to as a
race condition. This occurs when you have multiple unsynchronized
writers, commonly when multiple accessors are trying to do
read/modify/write and do no locking of the resource around the read/write.

> Difficulty: Ok so let's say you have a global and want multiple threads to
> read/write properly, but want to make no assumptions? Or more likely you
> have some other shared resource. Consider a lock! You can use their
> semaphores but those don't atomically test-and-aquire. You'll probably need
> to make sure that if re-entrantcy is unchecked your VI will be executed
> atomically.
>


The LV semaphores, mutexes, rendesvous, and queues are internally
implemented on top of OS resources and test and increment instructions,
so they are as atomic as one can get, even on multi-CPU computers. You
should use these to synchronize access when it is important to restrict
access due to there being more than one writer with no apriori
synchronization between them. Also keep in mind that non reentrant VIs
act as a critical section, guarded so that only one call is active at a
time. Reentrant VIs are safe as long as they only use local resources
like shift registers or control values, no globals or global data.

> However this is tough. That might be why globals are generally discouraged.
> Just like in any programming language, globals deal with shared resources
> which the average lackey does not understand.
>



One reason that globals are not overly encouraged is that their overuse
lead to diagrams that are difficult to read or debug -- the biggest
problem being race conditions between writers. Because the globals are
protected, they can also turn into a performance problem if used inside
of really tight loops. One solution you will frequently hear is to
switch to LV2 style globals. These are named this way, because globals
weren't built into LV until LV3. In previous versions, you constructed
your own global by making a VI with state information updated as a
side-effect of a call. Look at them in the examples folder for more
info. The benefit you can gain from them is to localize the access to a
single function, which automatically has a critical section, a much
simpler way of atomicity compared to semaphores, and since you have a
diagram there, you can place common access patterns into the global VI
and simplify your code and improve performance since the locked access
and copying is minimized.

Greg McKaskle
Message 5 of 7
(5,222 Views)
Thanks for your reply. Answers the technical aspects of LabVIEW concerning Atomicity as well as suggesting alternative use of the gloabls. I usually organize my globals into a file by function as well as write the vis to be used by the rest of the application to access the globals. They are not half hazourdously distributed throughout the application. I am very careful to avoid race conditions.

However, I am starting to see the benefits of the LV2 style globals. Maybe I'll convert over someday!
0 Kudos
Message 6 of 7
(5,222 Views)
I'm converted. Fundamentally I am not opposed to globals. But I have re-written some code with Labview2 globals and have found that this is definitely the way to go. By using actions or modes to the global vis, you can accomplish data encapsulation and define the methods that act on that data in an efficient modular manner.

Thanks for all of the advice!
0 Kudos
Message 7 of 7
(5,221 Views)