Race condition

From LabVIEW Wiki
Jump to: navigation, search

A race condition in LabVIEW occurs when two or more sections of code attempt to update a common variable. This often occurs when local or global variables are overused.

Before

Example Race Condition


The first 16 bytes of the file will be read by the left instance of Read Characters From File.vi. Offset will be set to 16, BUT the loop at the right may start and read 22 bytes from the default value of Offset rather than the expected 16.

After

Race Condition Eliminated Using Data Flow

Data flow will ensure that this VI reads 16 bytes from the beginning of the file, then 22 bytes at a time for 16 iterations.

Summary

Local and global variables can break the data flow model. If Read and Write instances of local or global variables exist on a single block diagram, they are often part of a loop structure. Variable instances should be replaced with a shift register and data written directly to the control. If the VI is not a UI (front panel visible when running), place the indicators outside the loop and update them once when the loop is complete.

External links