Use a functional global variable (FGV) to communicate between two parallel process loops contained within different VIs under the same target, and use a FGV to stop parallel loops with one "stop" button. The "functional" nature of the FGV means that you can create additional functionality beyond that of a basic global variable, e.g., counting and calculations that operate on the stored value.
Use cases
Communicate data, control, and status between two or more parallel process loops contained within the same target (“target-scoped”), either in the same VI or in different VIs
Make the latest value of a variable available to other process loops
Stop multiple parallel loops from a single control
Perform additional operations (the “functional” aspect of the FGV acronym) beyond merely storing the data, e.g., counting and calculations
Features
A functional global variable is subVI with these defining characteristics:
Uninitialized shift register – an uninitialized shift register on a while-loop causes LabVIEW to allocate storage for a single value that persists as long as the calling VI remains in memory
Single-iteration while-loop – the while-loop conditional terminal is wired to a “true” constant, therefore it only iterates once; the while-loop is merely a mechanism to hold the shift register
Case structure with enumerated control – the case structure selects an operation to perform on the stored value; “read” and “write” are the most basic operations, and additional cases can implement additional functions such as “increment”, “decrement”, etc.
Non-reentrant subVI – this execution mode ensures that only one instance of the subVI exists in the target; multiple instances of the subVI all refer to the same stored value
Memory efficiency – the stored value exists in only one place in memory, whereas each instance of a local/global variable reader creates its own copy of the data
Avoids race conditions – the stored value cannot be written while it is being read because the subVI can only be executed by one process at a time
Keep in mind
The subVI execution mode must be set to “non-reentrant” (open the subVI, press Ctrl+I, select “Execution” category, and choose “non-reentrant execution”); the other two reentrant execution modes create independent copies (“clones”) of the subVI, thereby eliminating the global variable aspect of the FGV
LabVIEW block diagram elements
The functional global variable is a design pattern as opposed to a set of built-in VIs. Refer to the example code below for details.
Example code
Connect your Academic RIO Device to your PC using USBLAN, Ethernet, or Wi-Fi. NOTE: Not all Academic RIO Devices have Ethernet and Wi-Fi connectivity options.
If using the NI myRIO 1950 or NI RIO Control Module start with the NI myRIO 1900 Archive.
Different IP address: Right-click on the "NI myRIO 1900" Device, choose "Properties", and then enter the new IP address
Different device:
Right-click on the top of the project hierarchy, select "New Targets and Devices", keep the "Existing target or device" option, and then find and select your particular device
Select all of the components under the "NI myRIO 1900" device: click the first one and then shift+click the last one
Drag the selected components to the new device
Right-click the "NI myRIO 1900" device and select "Remove from project"
Open three VIs: “RT Main”, “RT subVI-1”, and “RT subVI-2”
Run “RT Main”
The “fast counter” in Process Loop #1 of “VI one” increments once each loop cycle (100 ms)
The Boolean “reset” generated by Process Loop #2 of “VI two” resets the fast counter
The “slow counter” in Process Loop #2 increments each time the fast counter reaches 10 counts; this condition also enables the “reset” signal
Open the three “fgv - ” subVIs to observe the state of the three functional global variables “fast counter”, “reset”, and “all stop”
Stop all VIs by clicking the “Stop” button to set the “all stop” FGV