03-23-2005 09:17 AM
03-23-2005 09:35 AM
03-23-2005 10:08 AM
03-23-2005 10:12 AM
12-15-2006 08:59 AM
I agree with most of what Ryan says. I've been programming with LabVIEW since it was 1.0. Over time I have found that if your not careful with local variables data will get either lost or will be updated late. I have gotten away from using locals and went to properity nodes for small amounts of data or instrument calls.
Recently I was given that task to creat a program for a customer product which was incredibly hard since the documentation was incorrect and the customer was on the other side of the world. When I finally figured out the command syntax I used globals to store them and then present them to be entered into a file. I did not have any conflicts or race conditions. The program worked as advertised. The customer was happy and other people took the credit. Now they want me to disassemble the globals and use locals. Is there any thing gained by switching from one to the other.
Most of my programming is instrument control with data placed into arrays or clusters for reuse. I need to get my mind right.
Jr. Cigar
12-15-2006 10:14 AM
The problems with Global Variables aren't really specific to Global Variables, most of them refer to unprotected global data in general, particularly in an unbuffered manner. Therefore, you get the same problems using your controls to store data (i.e. using local variables or value propterty nodes). As a side note, last I checked, value property nodes actually have worse performance than local variables (and are otherwise the same, unless you are using value(signaling)) because they have the potential to cause an additional thread swap into the user interface thread. They are nice because they have the error clusters which make it easy to control your dataflow though. As far as globals vs locals, there really isn't a big difference. If you are using them exclusively to store data it probably makes more sense to use globals just so you don't end up with a bunch of unneccessary controls on your front panel. Both will have the same potential problems as far as memory efficiency (generally not a big deal on modern PCs), race conditions, and overflow/underflow. If you've already solved these problems using globals (which it is certainly possible to do, just requires a little more thought than using a safer mechanism) then you won't get any benefit by switching to locals. In general, while refactoring code is an admirable task, if it isn't broken, don't fix it.
If you do decide to replace the globals with another global data storage method, there are two I use regularly. The first is functional global variables (a.k.a. LV2 style globals). These are good for storing relatively static data that isn't being streamed between parts of the program. At their simplest (i.e. with just a "get" and a "set" command), these aren't actually any better than a regular global, but if you implement a functional global with robust commands that actually manages the data within the functional global (i.e. have an "increment" command rather than calling "get", incrementing the value, then calling "set") this provides you with some protection from race conditions and, depending on the implementation, the potential for more efficient memory usage. A good rule to follow is that any critical section of code (a section where you are performing operations while expecting the global value not to change) should be inside the functional global, which will then protect it like a semaphore. You can also implement buffering within a functional global to protect yourself from overflow or underflow, but that's really overkill, since if you need buffering you're better off using the second common global data storage/transfer mechanism, which is some form of queue (usually the basic queue is the best bet on a windows platform, but you can achieve the same thing using other types of queues such as Real-Time FIFOs or even user events).
The OOP capabilities in LV8.20 also offer some nice ways to organize your global data, to be honest, I've been so busy I haven't had a lot of time to play with them yet, but at the least they offer ways to protect your data similar to a functional global. Since the LV implementation passes by value however, you'll still need another global storage mechanism to actually store the objects.
If you're interested in more information on these, our training courses (particularly LV Basics II and LV Intermediate I) cover the proper use functional globals and queues.
Hope that helps,
Ryan
P.S. A tip for using the forums: it's generally not a good idea to post on old topics like this, since only people who are subscribed to the topic will notice the post and subscriptions expire after a while. Luckily I still had an active subscription for this post, but in general it's best to start a fresh one so everybody sees it.
12-15-2006 02:56 PM
12-26-2006 09:24 AM
12-26-2006 09:39 AM
An LV2 global is just what it is called - a global. That means that it is relevant for all the VIs that call it at the same time and the different VIs will use the same global at the same time and mess it up. To see this, open the VI hierarchy window and you will see that all the VIs are calling the same global.
Some options -
I would suggest avoiding option 2, because duplicating VIs is a problem when you want to change code. This also goes for your currently duplicated VIs.
If you want to have several instances of the same VI, you can set the VI to reentrant in the VI Properties>>Execution window or change it to a VI template, depending on what you want to achieve.
To learn more about LabVIEW, I suggest you try searching this site and google for LabVIEW tutorials. Here, here, here, here and here are a few you can start with and here are some tutorial videos. You can also contact your local NI office and join one of their courses.
In addition, I suggest you read the LabVIEW style guide and the LabVIEW user manual (Help>>Search the LabVIEW Bookshelf).
12-26-2006 12:28 PM