LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
vitoi

Global Variables with race-conditon-free option

Status: Declined
Proper programming practices for global variables are encouraged to help reduce race conditions.

There is a debate between the use of Global Variables and Functional Globals to make variables globally available. The reasons for using a Global Variable appear to be that they are easy to use and execute faster. The reasons to use a Functional Global appear to be that they reduce data copies and avoid race conditions.

 

I'm only talking about Functional Globals that set and get values. Action engines are a different thing and there's the standard implementation method.

 

I'm not too worried about data copies as my global variables are simple and memory is cheap. However, race conditions do worry me. I assume that for a Global Variable, even if someone is accessing the variable to write to it, a reader elsewhere can read at the same time and therefore either get the earlier or later value.

 

The idea is to provide an option on Global Variables to make them race-condition-free (that is, same behaviour as Functional Globals). When activated, a small padlock could appear on the global to let everyone know it's race free.

 

You would now have pretty well all of the benefits of Functional Globals and Global Variables. Thereby putting an end to having to produce a VI, put in a loop, shift register, states, etc, just to set and get a variable. Action engines on the other hand great and worth the effort!

 

Maybe this Global Variable behaviour should not be an option, but the only way it operates?

16 Comments
SteveChandler
Trusted Enthusiast

Peter_B, that is absolutely correct. vitoi, whatever happens inside the FGV or Action Engine is guaranteed not to be interfered with while that VI is executing. The race condition happens when two parallel processes are accessing the VI without any synchronization. These two loops demonstrate that. If the programmer expects what is set in each loop to be the same when it is read he is in for a surprise.

 

The thing about race conditions is that you might not notice any problem. Maybe it crops up when you enable highlight execution or upgrade LabVIEW.

 

race condition.png

=====================
LabVIEW 2012


vitoi
Active Participant

My main premise, now that I understand thigns better, is that a Global Variable and a Functional Global that only sets and gets, are exactly the same when it comes to race conditions. That is, simply substituting a Global Variable with a Functionla Global (and nothign else) will do nothing for race conditions.

 

(Functional Globals still have their place for Action Engines - wonderful things - but for all those many cases where there is only a set/get function, at least for simple variables, they serve no purpose.)

 

I think this idea can now be Declined, although it does offer educational value.

G-Money
NI Employee (retired)
Status changed to: Declined
Proper programming practices for global variables are encouraged to help reduce race conditions.
vitoi
Active Participant

I take it that when you state "global variables", that you mean Global Variables and Functional Globals that only set and get. I think it's important to make sure there's a realisation that Functional Globals that only set and get are not going to improve any race conditions.

AristosQueue (NI)
NI Employee (retired)

> I take it that when you state "global variables", that you mean Global Variables

> and Functional Globals that only set and get. I think it's important to make sure

> there's a realisation that Functional Globals that only set and get are not going

> to improve any race conditions.

 

Correct!

 

There were some who told me I answered too harshly in my first post, so I figure responding with an equally enthusiastic support at the close is appropriate. 🙂 

vitoi
Active Participant

And with an exclamation mark to boot! 🙂