Use a queue to send messages and data between two or more parallel process loops contained within a VI or other VIs. Queues also serve as the foundation for the "Queued State Machine" design pattern.
Use cases
Communicate data, control, and status between two or more parallel process loops contained within the same VI (“VI-scoped”) or between multiple VIs on the same target (“target-scoped”)
Establish point-to-point lossless communication (stream, message)
“Queued Message Handler” design pattern
“Queued State Machine” design pattern (maintains the state of the state machine)
Elements are inserted at the back of the queue (“enqueued”) by one process loop and removed from the front of the queue (“dequeued”) by the other process loop
High-priority messages may be inserted at the front of the queue
Queues may be referenced by a wire or by name
Queues support variable-size data types such as arrays and variants
Keep in mind
Any process loop can access a given queue, i.e., the queue can have multiple writers and readers, however, a single reader is recommended to promote reliable operation and avoid unexpected race conditions and deadlocks
LabVIEW block diagram elements
Locate these elements with "Quick Drop" (press Ctrl+Space and start typing the name); click on an icon to see more sample code that uses that element:
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.
Download and unpack the
rt_queue.zip (for use with NI myRIO 1900)
or the
NIELVISIII-rt_queue.zip (for use with NI ELVIS III)
archive, and then double-click the ".lvproj" file to open the project. NOTE: This project was written for a NI myRIO 1900 or NI ELVIS III connected by USBLAN at IP address 172.22.11.2.
If you are using a different IP address or another Academic RIO Device (Example: NI myRIO 1950 or NI RIO Control Module) do the following:
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"
Run the “RT Main” VI:
Process #1 inserts ten elements at 1-second intervals, waits for two seconds, and then dequeues the elements at a more rapid pace
Process #2 inserts two elements at 3-second intervals, with one of the elements at the front of the queue (illustrates multiple writers to the same queue)
Process #1 (a flat sequence structure) enqueues elements, pauses, and then dequeues all elements
Process #2 (also a flat sequence structure) inserts two elements, one of them at the front of the queue; illustrates multiple writers to the same queue
Process Loop #3 observes the state of the queue
Locate the “Queue Operations” subpalette
Create a named queue
Designate queue name by a string
Connect representative element to establish data type; value does not matter
Obtain original reference as a data wire
Enqueue and dequeue elements
Normally enqueued at the end (bottom) of the queue
Possible to enqueue directly at the front (top) of the queue
Multiple processes can access the same queue
Obtain new queue reference to existing queue by name
Refer to existing queue name by a string
Select option to avoid creating a new queue if name is mistyped
The new reference (by wire) is not the same value as the original
Recognize a slight cost in CPU effort and memory allocation with this method
Release reference when finished; does not destroy the queue itself
Observe the queue status and elements
View elements in the queue as an array
Obtain number of elements in the queue
Catch the error code to stop observer loop when queue no longer exists
Destroy the queue
Release the single remaining queue reference to destroy the queue