Jump to content

Global Variable Usage


Recommended Posts

I have 2 or 3 VI's running in parallel (some may be sub-vi's of others) and i'm wondering how bad is it to use global variables in them. My needs are very simple. For example, in one program i am writing to variable X inside a while loop, and in a second program, I am using variable X to calculate variable Y. Honestly that's as simple as I need things to be. In total I might need 10 global variables. The VI's themselves are complex and they must be separated, but this is what I'd like to do with regards to global variable usage. My main concern is that my loops run for quite a while so the memory is very 'active'. On the LV website, there is a document that states that, with respect to global variables in a loop, 'labview reclaims the memory after each iteration of the loop.' so do i have much to worry about?

Link to comment

QUOTE(sm07 @ Jul 26 2007, 09:40 AM)

I have 2 or 3 VI's running in parallel (some may be sub-vi's of others) and i'm wondering how bad is it to use global variables in them. My needs are very simple. For example, in one program i am writing to variable X inside a while loop, and in a second program, I am using variable X to calculate variable Y. Honestly that's as simple as I need things to be. In total I might need 10 global variables. The VI's themselves are complex and they must be separated, but this is what I'd like to do with regards to global variable usage. My main concern is that my loops run for quite a while so the memory is very 'active'. On the LV website, there is a document that states that, with respect to global variables in a loop, 'labview reclaims the memory after each iteration of the loop.' so do i have much to worry about?

You could use a Q, where you write data in one of the VI's and read it elsewhere. Q's are very easy to use, and will make sure you don't loose (overwrite) any data that you haven't used in your consumer VI.

If you don't care about missing data, then maybe use a notifier. See LV examples for more ideas.

If you must use globals, then use a functional global (an unitialized shift register VI with read/write access), as the native LV globals tend to make copies of the data.

Neville.

Link to comment

Unless you're constantly building arrays or concatenating strings in a loop, your memory consumption should probably not be a problem. That is assuming you don't have any very large structures (tens or hundreds of MB).

The main reason for not using globals is that it's usually fairly easy to create race conditions which will cause bugs in your code.

Other options of transferring data and synchronizing include queues and LV2 style globals.

Link to comment

QUOTE(sm07 @ Jul 26 2007, 02:50 PM)

All of my data are either individual 'double's or booleans...and there are only about 10 of them in total

If the global is written only in one place, and only read elsewhere, it might be acceptable. The problem with this implementation is that it limits future flexibility in the program, and programs *always* seem to grow.

Bite the bullet and use a better structure. You'll learn the pattern for the structure (worthwhile in and of itself!), and never have to worry about what you (or someone else) will do in the code in the future.

Joe Z.

Link to comment

I went global-happy when I started using LabVIEW and regretted it later. It became such a hassle to keep track of them that whenever I would update a program I would start stripping them out. I think I updated the last program that had globals left in it about two weeks ago. It was a lot of work to take them out, but it would have been a lot more work to keep them there and keep them straight.

Link to comment

QUOTE(Topher @ Jul 26 2007, 06:52 PM)

I went global-happy when I started using LabVIEW and regretted it later. It became such a hassle to keep track of them that whenever I would update a program I would start stripping them out. I think I updated the last program that had globals left in it about two weeks ago. It was a lot of work to take them out, but it would have been a lot more work to keep them there and keep them straight.

Confessions of a global-holic

"

Hi my name is Ben and I was a global-holic.

I used my last global in 1999.

My first big project include a super-cluster stored in a global.

I learned about excesive data copies and race conditions. :oops:

I have been global free for about eight years now.

"

:rolleyes:

Ben

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.