What is a Functional Global Variable?

Updated Sep 30, 2019

Reported In

Software

  • LabVIEW
  • LabVIEW NXG

Issue Details

What is a Functional Global Variable?

Solution

A Functional Global Variable (FGV) is an often-used design pattern. An FGV is basically a non-reentrant VI featuring a while loop that iterates once, and has an un-initialized shift register. The purpose of this construction is to retain data between consecutive calls to the FGV.

FGVs can e.g. be used instead of global variables, mainly for two reasons:
  • Additional tasks can be performed when changing the stored value(s). Therefore, FGVs are sometimes also referred to as Action Engines (AE).
  • If used in an unsafe, accidental manner, normal global variables can lead to race conditions in your code. FGVs can help preventing those.

Additional Information

Race conditions can occur when two pieces of parallel code modify the same variable - they could read the same variables's value at the same time, but then the piece of code saving its new value last would overwrite the values stored by the faster piece of code. Functional Global Variables can be used to protect such actions that modify data. Please note that all critical sections of code must be protected by a non-reentrant FGV as a whole; this ensures that only one action executes at a time, so conflicting operations can never perform in parallel.

As a bad example, the following two parallel operations provoke a race condition, as the critical read-modify-write operations are implemented outside of the FGV:


In contract, in the following good example the critical read-modify-write sections of code have been implemented as actions of the FGV. As the FGV's non-reentrancy setting ensure only one action executes at a time, the two calls will always execute after each other, preventing a race condition.


Find a description on how to implement an FGV in the Functional Global Variables paragraph of Suggestions for Using Execution Systems and Priorities - LabVIEW 2018 Help.
A more detailed description of FGV's is available here: Functional global variable (FGV) - RIO Developer Essentials Guide for Academia