LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

is it a proper way to use queue for consumer/producer model?

Solved!
Go to solution

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

 

qued.png

 

when the case box is false, the code will be

 

qued1.png

 

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.

0 Kudos
Message 1 of 7
(2,854 Views)

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...


Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 2 of 7
(2,841 Views)

@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 😞

0 Kudos
Message 3 of 7
(2,834 Views)
Solution
Accepted by dragondriver

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.


I am not allergic to Kudos, in fact I love Kudos.

 Make your LabVIEW experience more CONVENIENT.


0 Kudos
Message 4 of 7
(2,827 Views)

 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

0 Kudos
Message 5 of 7
(2,825 Views)

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).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 7
(2,791 Views)

Thanks all. I add the while loop to the consumer part and it works now 🙂

0 Kudos
Message 7 of 7
(2,759 Views)