07-31-2013 11:06 PM
Hi all,
I am following the example of consumer/producer model to use the queue to synchronize the following process: The producer is a loop to produce N numbers, I will put every generated number into an array and after every 5 numbers generated, I put the array into the queue and pass it to the consumer. I have to wait the consumer use up the data and it will then remove the element from queue so the producer will get a chance to produce another 5 numbers. Since I set the maximum size of the queue to be ONE, I expect the producer and consumer take turns to produce / consume all five numbers and pass the chance to the other. Here is my code
when the case box is false, the code will be
For the first 5 numbers, the produce will generate every thing right and put that into the array, and it will pass the array to the quere so the consumer will get a chance to loop over the array. I except the procude's loop will continue only when the queue is available (i.e. all elements are removed), but it seems that once the consumer start the loop the produce 's loop will continue (so the indicator x+1 and x+2 will show numbers changed). But it is not what I want, I know there must be something wrong but I can't tell what is it.
Solved! Go to Solution.
07-31-2013 11:45 PM
Two problems:
1. Get rid of all the sequence structures. None of them are doing anything but enforcing an execution order that would be the same without them.
2. You don't have a consumer loop. You check the queue 1 time, loop through what you got and then don't look for anything more.
Recommendation: Start with the Producer/Consumer design pattern template that ships with LV and experiment with that.
Mike...
08-01-2013 12:15 AM
@mikeporter wrote:
Two problems:
1. Get rid of all the sequence structures. None of them are doing anything but enforcing an execution order that would be the same without them.
2. You don't have a consumer loop. You check the queue 1 time, loop through what you got and then don't look for anything more.
Recommendation: Start with the Producer/Consumer design pattern template that ships with LV and experiment with that.
Mike...
As you said in 1, the sequency structure enforcing the execution order, that's why I put it there, in this example, to put the issue simple, I replace the complete code with number increase, in the real case, the first +1 and +2 must be executed in that order.
For the second one, yes, it is my fault to remove that while. I actually start from the example of Producer/Consumer design pattern template, but I didn't pay attention to the while loop in the consumer part. I put it back but I still don't get it work 😞
08-01-2013 12:41 AM
dragondriver wrote:As you said in 1, the sequency structure enforcing the execution order, that's why I put it there, in this example, to put the issue simple, I replace the complete code with number increase, in the real case, the first +1 and +2 must be executed in that order.
Mikeporter mentioned:
1. Get rid of all the sequence structures. None of them are doing anything but enforcing an execution order that would be the same without them.
So even if you remove the sequence structure, there will be a fixed & defined execution order and that is because LabVIEW follows DATA FLOW MODEL.
Data Flow Model (specifically in context of LabVIEW): A block diagram node executes when it receives all required inputs. When a node executes, it produces output data and passes the data to the next node in the dataflow path. The movement of data through the nodes determines the execution order of the VIs and functions on the block diagram (Click here for reference).
Now in your code, just removing the sequence structure will not make sure that the execution order will gonna remain same but you need to do few very small modifications (like pass the error wire through For loop, before it goes to 'Dequeue Element' node).
Coming to the main topic: is it a proper way to use queue for consumer/producer model?
The model you're using (and calling it as consumer/producer model) is way too deviated from the original consumer/producer model model.
@dragondriver wrote:
For the second one, yes, it is my fault to remove that while. I actually start from the example of Producer/Consumer design pattern template, but I didn't pay attention to the while loop in the consumer part.
While loops (both Producer & Consumer) are the essential part of this architecture and can't be removed. You may want to start your code again using standard template.
08-01-2013 12:42 AM
its still not clear what do you want
if you want your consumer sholud use all the element and after that only the producer loop should again enqueue
you should use some kind of communication scheme through consumer to producer.
and i don't see any use of dequeue function again in your consumer loop what are you expecting because without timeout this function will wait forever for elements
08-01-2013 07:30 AM
You also should not be using the Preview Queue. Use the Dequeue. If you don't specifiy a timeout, the consumer loop will just sit there waiting for data to enter the queue. As soon as the data comes in it will process it. And I would leave the size of the queue undefined (not wired).
08-01-2013 07:31 PM
Thanks all. I add the while loop to the consumer part and it works now 🙂