LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how does a global cause a race condition?

Hello All,
             I am learning about variables and realised I havent used globals and have started to investigate for a project. I am aware theres the standard global and lv2 globals, the lv2 are preferred as they dont cause a race condition. I hope I am right in thinking the race condition could be seen by opening 'System Monitor' which would indicate 100% CPU usage.
 
Could someone explain how the global causes a race condition and why the lv2 global doesnt cause one. Ideally a code snippet if possible please.
 
Many Thanks
 
Regards
 
Chris
0 Kudos
Message 1 of 4
(3,011 Views)

Chris,

A race condition has nothing to do with CPU usage. (See http://en.wikipedia.org/wiki/Race_condition this definition from wikipedia.) 

If you have a race condition, it simply means that you might get unexpected results because you cannot predict the order of events. For example, if you read a global variable, you cannot be sure that the global variable contains the most recent data or even if it ever has received data at all. Small, seemingly irrelevant changes in the code could possibly change the order of events, thus changing the results in an unpredictable way..
 
Even lv2 globals are not immune to race conditions but you can equip them with more intelligence to internally keep tabs on things.  
0 Kudos
Message 2 of 4
(3,000 Views)
Race conditions most often occur when multiple threads or processes are reading and writing to a single data value. Notice I said reading and writing since having a single writer will alleviate this problem. Here is an abstract example to consider:
Lets say that your bank stores your account balance in is single global called BALANCE, and both you and your spouse have an ATM card to this account.  When you use the card it calls the withdraw function (BALANCE = BALANCE - cash)  if either you or your spouse uses the card there is no problem but what if you both use it at the same time deciding to withdraw $200.00 each?
you both read the BALANCE variable which lets say is $1000.00.  You decrement your copy of the balance BALANCE = 1000 - 200 and update the variable with the new balance.  The final amount will be $800.00 but it should have been $600.00.  Ironically you might think the global saved you $200.00 but it does yield the wrong answer.  If you can avoid using globalization then avoid it.  Especially in LABVIEW where you have a data flow model of programming globals break the flow of data and can often be replaced with other data communication methods which provide a higher degree of synchronized data flow like queues.  If you are seeing nondeterministic results (looks random) this is a sign that you have uninitialized variables (rare in Labview) or you have a race condition.  Hope this example helped some,
Paul
Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 3 of 4
(2,989 Views)
Here is a race condition tutorial vi:
 
- tbob

Inventor of the WORM Global
Message 4 of 4
(2,977 Views)