LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Global variables or single process shared variables?

Solved!
Go to solution

Since the introduction of shared variables, whenever I needed a global variable, I have used single process shared variables. But I have now started going back to using the old global variable as I think there are some significant drawbacks to the single process shared variable. These are the ability to search for instances of the variables and also the ability to see or change the value of the variable (OK, we have the variable manager, but I have found it slow and unstable). My question is, is there any reasons for using the new single process shares variables over the old global variables?

0 Kudos
Message 1 of 12
(21,996 Views)

Hi Terje,

The use of single process shared variables is similar to a local or global variable, but allows you to share data across a network. A shared variable can be single-process or network-published.

If you use a single-process shared variable, you can later change to a network-published shared varibales.

 

Using a global variable to share data among VI's on the same computer especially if you do not use a project file. use a single-process shared variable if you may need to share the variable information among VI's on multiple computers in the future.

 

Another thing to consider using is functional global variables. A functional global variable usually has an action input parameter that specifies which task the VI performs. The VI uses an uninitalized shift register in a While Loop to hold the result of the operation.

The advantage of this over a global variable is that you can control access to the data in the shift register. They are especially useful when implementing complex data structures, such as a stack or queue buffer.

Ashish Naik
Automotive Business Development Manager
National Instruments UK
Message 2 of 12
(21,993 Views)

Hi Ashish

Thanks for your fast reply. I’m aware of the network-published feature of the shared variable and also functional global variables, but sometimes a simple global variable is all I need. A typical example of this it when I need to stop multiple parallel loops which can be embedded in multiple subVIs. Here I usually only have one writer and multiple readers so a race condition is not an issue. As I understand the single process shared variable has the same function as a global variable. I can see the advantage that if I would like to increase the scope of the variable it is simple just to switch a property, but is that the only advantage of using a single process variable over a global variable?

0 Kudos
Message 3 of 12
(21,978 Views)
Solution
Accepted by topic author Terje

Dear Terje,

As you are only using the variables on one system. There is no advantage to using single process shared variables over global variables.

 

Infact a global variable uses slightly less processing power than a global variable as in effect the implementation of a single-pocess shared variable is a global variable with time-stamping functionality.
If you do use a single process shared variable, if you do not need the time-stamping functionality, then disable it to use less processing power.

Ashish Naik
Automotive Business Development Manager
National Instruments UK
Message 4 of 12
(21,969 Views)

Terje,

 

well, there is one advantage of the single process shared variable which might be of interest:

It has an error in/out terminal. This is great for synchronizing the access to the variable.

But it can have a disadvantage too: what do you do in the unlikely case that the variable creates an error??

 

hope this helps,

Norbert 

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 5 of 12
(21,934 Views)

I have to agree with Norbert as the error cluster is good for data flow.

 

I wish NI would add error in and error out to Local and Global variables call if for nothing else than DATA FLOW.

 

I will use the value property node as a local variable just to have the data flow control when I read or write to it.

 

Omar

0 Kudos
Message 6 of 12
(21,904 Views)

OmarGator wrote:
[...]

I will use the value property node as a local variable just to have the data flow control when I read or write to it.

 

Omar


Omar,

 

if you have to optimize your code in regard of execution times, you shouldn't go for property nodes. Those have a great negative impact on the performance in regard of timing.

 

Norbert 

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 7 of 12
(21,864 Views)
If you want some benchmark information on globals vs. shared variables, check out this post.  It also explains why you really shouldn't use globals for anything more than a look-up table and offers good alternatives.  If you need more information, let us know.
Message 8 of 12
(21,838 Views)

Thank you all for your replies.

DFGray, I found your post very interesting and will certainly follow your recommendations in the future. 

 

Do you recommend to never use Global variables, even for simple tasks? When I say simple tasks I am typically thinking of the example I mentioned, using them to stop multiple parallel loops which can be distributed in several subVIs. Here, I only have one writer so race condition is not an issue and the amount of data is low (only a boolean) so the data copying during reads would not influence memory consumption too much.  

 

I am only asking because I find Global variables easy to use and that they make the block diagram looking simpler and easier to read than when involving queues. (at least if you are not considering the broken dataflow)

Terje

 

0 Kudos
Message 9 of 12
(21,816 Views)
The case you mention is a valid use case for globals.  Any time you can guarantee that you will not have a race condition (usually means one writer), a global is a good option, since it is very fast and very easy to use.  I should probably modify the article to generalize to one writer instead of look-up table.  Thanks for your feedback.
0 Kudos
Message 10 of 12
(21,795 Views)