09-13-2011 11:48 AM
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.
09-13-2011 11:55 AM
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
09-13-2011 11:59 AM
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.
09-13-2011 12:05 PM
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.
09-13-2011 12:06 PM
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.
09-13-2011 12:12 PM
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.
09-13-2011 12:18 PM
Hi Steve,
I like your example. I want to see whether there are other differences for using these two approaches.
09-13-2011 12:20 PM
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.
09-13-2011 12:24 PM
@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
09-13-2011 12:36 PM
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.