LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Should I use Producer/Consumer Design Pattern (Instead of Globals)?

Solved!
Go to solution

Hello everyone.

 

I have an application which several loops in it. One loop reads serial data and parses it for use in other loops, another loop acquires data via DAQmx and runs computations on it for use in other loops, another formats the data from both loops and displays it, and the last one tests the data. I was wondering if this is a good candidate for the producer/consumer design pattern.

 

Right now I use global variables to send data from the serial and DAQmx loops to the other loops. I use action engines for various parts of the program that needs to be used everywhere, like the serial port. The test loop only needs the most current data to run it's test.

 

Thanks for any suggestions!

0 Kudos
Message 1 of 6
(3,245 Views)
Solution
Accepted by lukepike
The short answer would be yes. If nothing else using queues to pass the data will allow your various loops to be idle if no data is present. Using globals your loops must continually poll to see if the data has changed. You are always beter off if you can make your application more event driven rather than using polling.


Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 2 of 6
(3,240 Views)

I stopped using global variables a few years back, and I can't honestly find a use for them any more. Race conditions are the primary reason not to use globals (or locals). And as Mark mentioned, using Queues (or Notifiers) allow loops to remain quiet until they have work or data to process/display.

Richard






0 Kudos
Message 3 of 6
(3,229 Views)

Producer/Consumer design pattern is my favorite!  One "gotchya" when using this loop is if your producer works consistently faster than your consumer loop does.  This can lead into issues.  In general, the Producer/Consumer design pattern works well when "hic-ups" on your system occure.  You will notice this on graphs that may be consuming data in the consumer loop.  There may be a momentary pause in data being shown on a graph control, and then a burst of data comes through.  I strongly encorage using a complex data type that has a time/date stamps on your data (such as the Waveform or Signal datatype).  Without queuing the data, when your system "hic-ups" you may have lost that data.

 

The "gotchya" can be a problem if you have alarms or triggers that occure when particular data is seen.  For instance, you may have to shut something off if a temperature exceeds a particular value.  I use globals in these cases as I'm interested in only "the most recent" data.  For saving, performing clacs, and displaying data I use producer/consumer.

0 Kudos
Message 4 of 6
(3,210 Views)
Beginning in 8.6 you could easily replace your globals with lossy queues with a size of one. The process reading or waiting on this queue will only get the latest data since the queue size is one and it is configured to purge old data from the queue once the queue hits its maximum size. A notifier could also be used to accomplish this.


Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 5 of 6
(3,204 Views)
Thanks for all the replies! I'll be sure to look into that lossy queue with a size of one, that looks like what I want. Thanks again!
0 Kudos
Message 6 of 6
(3,174 Views)