Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 36 ยท State Machines and Design Patterns

The Producer/Consumer Pattern

Learn the producer/consumer design pattern in LabVIEW: two parallel loops linked by a queue so slow work never blocks acquisition or the user interface, with queue VIs explained.

A single loop that acquires data, writes it to disk and updates a graph will eventually fall behind: file I/O takes a variable amount of time, and while it runs the acquisition buffer fills. The producer/consumer pattern fixes this by separating who makes data from who uses it and connecting them with a queue.

LabVIEW is not available on this site. The block diagram is described precisely, and a runnable Python stand-in using a queue and a simulated clock shows the same behaviour.

The shape of the pattern

Two (or more) independent While Loops run in parallel, because LabVIEW runs loops without a data dependency on separate threads automatically:

  • The producer loop acquires data or reacts to the user and enqueues elements.
  • The consumer loop dequeues elements and processes them (write to file, analyse, display).
  • A queue reference wire connects them. It is created before the loops and destroyed after.
flowchart LR
    P["Producer loop<br/>DAQ read, button events"] -- "Enqueue Element" --> Q[("Queue")]
    Q -- "Dequeue Element" --> C["Consumer loop<br/>write file, analyse, display"]

The queue buffers data, so a slow consumer does not stop the producer until memory runs out.

The queue VIs

All are on the Synchronization > Queue Operations palette:

VI Purpose
Obtain Queue Create (or look up by name) a queue. The element data type is set by the constant wired to element data type; a cluster or enum-plus-variant works well.
Enqueue Element Add an element at the back. Has an optional timeout and lossy variant.
Dequeue Element Remove the front element; waits (blocks without using CPU) until one arrives or the timeout expires (default -1 = forever).
Flush Queue Dequeue everything at once.
Preview Queue Element Look at the front element without removing it.
Release Queue Release the reference. With force destroy the queue is destroyed and blocked Dequeue calls return an error, which is how you tell the consumer to stop.

Because Dequeue Element blocks until data arrives, the consumer needs no Wait and sleeps efficiently.

Stand-in: two loops and a queue

Python Producer and consumer with a queue (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The producer finishes well before the consumer but loses nothing: the queue held the backlog. In LabVIEW you can watch queue size with Get Queue Status, which is useful for debugging if consumers lag.

Stopping both loops cleanly

A frequent beginner problem: pressing Stop ends the producer but the consumer waits forever in Dequeue. Safe approaches:

  1. Sentinel element. The cluster has a command enum (Data, Stop); the producer enqueues Stop on exit; the consumer stops on it. Clean, because the consumer finishes everything queued before the stop.
  2. Release the queue (force destroy). Dequeue then returns an error, and the consumer's loop condition stops on error. Faster but discards the backlog.
  3. Timeouts plus a shared stop flag. Works but wastes polling and delays shutdown.
Python Stopping with a command element

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Several consumers, several producers

You can have one queue per consumer (a UI queue, a logging queue, an analysis queue) so one loop's speed does not affect another. Several producers can enqueue to one queue, for example front-panel events and a timed trigger, giving natural serialisation. This idea, with commands that carry data, becomes the queued message handler.

Related primitives for other needs:

  • Notifiers: latest value, many readers, no history (status broadcast).
  • Semaphores and rendezvous: mutual exclusion and synchronising loops at a point.
  • User events: queue-like messages that an Event structure can receive (event-driven UIs).
  • Channel wires (Messenger, Stream, Tag and others): graphical wrappers for common patterns, available in recent LabVIEW versions.

Choosing the queue element

Use a cluster with a typed enum command and a Variant payload when many message types share a queue; the consumer's Case structure switches on the enum and converts the variant with Variant To Data. For one data type only (an array of waveforms) use that type directly: it avoids conversion cost.

An unbounded queue consumes memory if the consumer is slower than the producer for a long time. For continuous acquisition, set a maximum size on Obtain Queue and use Lossy Enqueue or a timeout, or add a monitor that reports the backlog.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main advantage of the producer/consumer pattern?

    Parallel loops linked by a queue let each part run at its own pace.
  2. 2. What does Dequeue Element do when the queue is empty and the timeout is -1?

    It blocks efficiently until data or an error arrives.
  3. 3. Which is a clean way to stop a consumer after it finished the backlog?

    A stop element is processed in order, so earlier data are handled first.
  4. 4. Why give each consumer its own queue?

    Separate queues isolate slow work such as disk I/O from fast work such as display updates.

Summary

  • Producer and consumer loops run in parallel and communicate through a queue.
  • Dequeue Element blocks without wasting CPU; Enqueue never waits unless the queue is bounded.
  • Stop consumers with a command element or by releasing the queue.
  • Use separate queues for separate consumers and watch queue growth.

Next: The Queued Message Handler