LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Producer/consumer pattern, why queue?

I used this concept before but I used local variables to send data between loops. Right now, I'm working on a project it will deal with this pattern. I want to do it in the formal way: using queue to send data. But I really don't know what is the advantage of of doing it this way over using local variable. Could some one with the knowledge explain to me?

 

Thanks in advance.

0 Kudos
Message 1 of 28
(5,333 Views)

A queue holds data in the order that the data was received.   By using a queue, the producer and consumer loops can run a different speeds.  The producer can generate data at will, while the consumer can take its time processing the data.  There is no real time dependency between loops. 

 

Better explanation here: http://zone.ni.com/devzone/cda/tut/p/id/3023

>

"There is a God shaped vacuum in the heart of every man which cannot be filled by any created thing, but only by God, the Creator, made known through Jesus." - Blaise Pascal
Message 2 of 28
(5,329 Views)

One reason is that local variables can drop data where a queue will give you every data point.  There is an example that ships with LabVIEW, I will try to find it and post the name.

Message 3 of 28
(5,325 Views)

I think this should one difference: data will not get lost for using a queue. For local variable, if the producer runs faster, a new value will replace the old one before it's being taken.

 

My loops will run at very different rate and I will need to have two loops of being producer and consumer to each other. I'm still think of how to do it.

0 Kudos
Message 4 of 28
(5,318 Views)

Queues are more efficient for passing data than local variables.  Also, using a queue, you can separate the producer and consumer into separate loops.  This lets you run your consumer at a higher priority, or in a different execution subsystem.  A local variable requires that both loops be in the same VI.

Message 5 of 28
(5,317 Views)

vt92 beat me to it but I was going to say it in a slightly different way.

 

Think of a pizza delivery place. You call and order a pizza and they put the order on a stack. The cook takes the oldest order from the stack and makes the pizza.

 

Now imagine that instead of a stack of orders they used a whiteboard. Every time that an order comes in they erase the board and write the new order on it. While the cook is busy making a pizza he does not see the whiteboard. If three people called and made orders while the cook was busy making a pizza he will only see the most recent order.

 

Two people will go hungry.

=====================
LabVIEW 2012


Message 6 of 28
(5,310 Views)

Hi Steve,

 

I like your example. I want to see whether there are other differences for using these two approaches.

0 Kudos
Message 7 of 28
(5,306 Views)

The other side of that analogy is that if no other orders come in when the cook is making that first order, he finishes it, looks at the white board, the old order is still there and he winds up making a second duplicate pizza.

Message 8 of 28
(5,301 Views)

@guangdew1 wrote:

... I want to see whether there are other differences ...


A 'Local Variable' will create a data copy, I am of the opinion that a 'local variable' should have its name changed.

If either the producer or consumer are notably quicker than the other, there is a potential race condition in adding and removing a queue element, and even a chance of losing a generated event.

As other have said, a queue is an ordered stack

 

_____________________________
- Cheers, Ed
Message 9 of 28
(5,297 Views)

As I mentioned earlier, this is going be two way data traffic. The main loop is going to deal with user interface and send commands to the other loop. The other loop will try to grab data and send related information/status back to the main loop. Then the main loop will send another command. I'm thinking in this case, they are producer/consumer of each other. I will use two queues for the task.

0 Kudos
Message 10 of 28
(5,286 Views)