From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

producer consumer loop question

Hi,

In a producer consumer design pattern, I notice that the consumer loop runs 1 more iteration than the producer loop. Does anyone know a way around this.

Thank you,

-Tim
0 Kudos
Message 1 of 4
(2,759 Views)
Tim,

Your question is very vague.

What do you want to achieve?  Is this behaviour leading to problems?

Can you post an image of your code?

I don't think that a consumer loop and a producer loop need actually be linked in the number of iterations they perform.  I've many VIs where one can iterate many times more than the other, but this is all a question of design.

Producer/consumer is a relatively broad description of many different correlations between parallel structures.  We need more information to give a concrete answer which may bring you further.

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 2 of 4
(2,751 Views)
The behavior you are talking about doesn't always occur with the Producer/Consumer design pattern, since the communication between the loops can take various forms. The default implementation for the design pattern seen in the LabVIEW templates and online help, however, will have the effect you are mentioning. Here's why and what you can do if this really is a problem for you:

The LabVIEW templates implement the P-C design pattern such that the Consumer loop is automatically shut down when the Queue gets destroyed. It does this by wiring the error terminal from the Dequeue Element function into the terminal condition input of the while loop. When the Queue gets destroyed following the producer loop, this causes the Dequeue Element function to error out which shuts down the loop. This is why (most likely) your Consumer loop runs once more than the Producer Loop. If the Producer Loop sends N messages, the Consumer Loop must run N times to handle these messages, then it must run once more to wait for the Release Queue function.

This shouldn't ever be a problem. You might also notice another thing the templates do is wire the Error Out from the Dequeue Element function in the Consumer Loop into a case structure, and then only use the dequeued element value if there isn't an error (if the Queue hasn't been destroyed). So you shouldn't have to worry about actually processing dummy default data if you follow this guideline.

Now, if it will still be a problem to operate in this manner, then you will have to build the shutdown command from the Producer loop into your communication mechanism. For instance, you could send a cluster with a boolean and your data to the Consumer Loop. A true boolean value means this is the last iteration, and the consumer loop should shut down after processing the data. There's a million ways to accomplish this, but as I said, it probably isn't necessary to even bother.

Message Edited by Jarrod S. on 01-15-2007 10:29 AM

Jarrod S.
National Instruments
Message 3 of 4
(2,746 Views)
Nice answer Jarrod,

I've only recently upgraded to LV 8.20 from 6.1 (where there are no producer consumer templates AFAIK) so I've been rolling my own so to speak, hence my answer.

Either way, my post just got irrelevant.  I think Jarrod's got it nailed.

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
Message 4 of 4
(2,719 Views)