09-30-2009 09:39 AM
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!
Solved! Go to Solution.
09-30-2009 09:44 AM
09-30-2009 09:59 AM
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.
09-30-2009 01:34 PM
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.
09-30-2009 02:07 PM
09-30-2009 07:55 PM