Company Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Avoiding Race Conditions

LabVIEW 8.5 Help
August 2007

NI Part Number:
371361D-01

»View Product Info

A race condition occurs when two or more pieces of code that execute in parallel change the value of the same shared resource. Because the outcome of the VI depends on which action executes on the shared resource first, race conditions cause unpredictability. Often, race conditions occur with the use of local and global variables or an external file, although race conditions can exist any time more than one action updates the value of the same stored data. The following block diagram shows an example of a race condition with a local variable.

The output of this VI, the value of local variable x, depends on the order in which the operations run. Because each operation assigns a different value to x, there is no way to determine whether the outcome will be 7 or 3. In some programming languages, a top-down dataflow paradigm ensures execution order. In LabVIEW, you can use wiring to perform multiple operations on a variable while avoiding race conditions. The following block diagram performs the same addition operations as the previous block diagram, but replaces the local variable with sequential wiring, eliminating the race condition.

If you must perform more than one action on a local or global variable, make sure you determine the order of execution of actions. You also can use a single functional global variable instead of multiple local and global variables. For example, if you want to read from stored data and write to the same stored data, you can use a functional global variable to clarify which action happens first and make sure both actions do not execute at the same time. Otherwise, the VI performs the read action first in some executions and the write execution first in others, which results in two possible outcomes.


Resources


 

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
Add Comments 1 2 3 4 5 submit