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: 

Looking for a good queue tutorial

I am looking for a good tutorial on using queues, specially in the producer consumer program model.

 

I have use a couple queued state machines and did not really see much of an advantage over the standard state machine beyond it saves you the two minutes it takes to create the typedef.

 

I have looked at the producer consumer examples and sure they work but I do not understand why. The producer consumer model does not seem to follow the data flow paradigm. The data collected in the producer loop just gets magically transported to the consumer loop? I can't accept that.

========================
=== Engineer Ambiguously ===
========================
Message 1 of 9
(12,557 Views)

The queue passes that information by reference, not magic. Smiley Wink

 

So at first glance it might seem to break data flow, but it does not because the reference is passed by a wire. At least... that's what I think. Am I wrong, anyone?

 

Also, I see an advantage of using the queued state machine over the standard state machine and that is I can enqueue many states at once, and I have more control over what the state machine is going to do in the long run. Also, you can make a type def'd cluster that contains your state control enum, but pass other data that is state specific in there as well to be used in specific states.

0 Kudos
Message 2 of 9
(12,554 Views)

@RTSLVU wrote:

I am looking for a good tutorial on using queues, specially in the producer consumer program model.

 

I have use a couple queued state machines and did not really see much of an advantage over the standard state machine beyond it saves you the two minutes it takes to create the typedef.

 

I have looked at the producer consumer examples and sure they work but I do not understand why. The producer consumer model does not seem to follow the data flow paradigm. The data collected in the producer loop just gets magically transported to the consumer loop? I can't accept that.


 

Dataflow refers to the order of execution and that something will execute when all inputs are available to it. Therefore, if all inputs are available to a queue (the reference, the error cluster and the timeout which is optional) it will then execute. It doesn't care whether or not the reference is a global, or a split wire. As long as it's available it will execute..The advantage of the queued state machine is when you may want to execute 5 states based on 1 condition, and 5 other states based on the opposite condition. Well, you only have to check the condition in 1 place and you queue up all 5 states right there. In a state machine you would have to continually check that condition every time you left a state to determine which state to go to next. You will find the more you program, that in big applications queued state machines will allow you to reuse states in multiple different orders which makes things much more flexible.

 

Another option is a producer-consumer queued state machine. This is somewhat the best of both worlds. See the openg commander source code here for an example of that. It helps to see real world examples to really understand things best. Look at that code and see if you can determine how you would do it with a regular state machine. I'm sure you will be hard pressed to find a way to reuse those states without doing a lot of condition checking to see which state to go to next.

 

Hopefully I answered your questions.

 

Message 3 of 9
(12,547 Views)

Here is a great article with example that helped me wrap my head around queued state machines. It's pretty in depth. Hope it will help you too.

 

Now Using LabVIEW 2019SP1 and TestStand 2019
Message 4 of 9
(12,521 Views)

Actually you are right- Queues DO break dataflow!   With a queue you can run into race conditions if you use them incorrectly and no wires are needed between the producer and consumer of a queue! If you name the queue, when you get a referance to the queue LabVIEW first checks if there is a queue of that name in the application space- if there is not it sets up a area in memory to put the queue data and really, thats what passes on the queue out, a referance to the memory where the queue exists.  If the named queue allready exists queue out will be a referance to the SAME area of memory so all queue functions point to 1 copy of the queue data.

 

Steven Mercer put in some thought about preventing reace conditions with queues so the palatte functions behave pretty well if you say try to dequeue from an empty queue it waits and if you destroy a queue any dequeue operations that are waiting exit with an error. 

 

The shipping examples are actually REALLY good at explaining how to use the queues and how to avoid some trouble with them.


"Should be" isn't "Is" -Jay
0 Kudos
Message 5 of 9
(12,487 Views)

Sorry to get into the discussion after a year. But, I thought I could get an answer to my question as it is relevant here.

 

In the QSM article that was posted,I have read it before , never understood it before but do now. But, one thing is sure confusing. It looks like only state machine states are enQd but the deQ funtion deQs data and state. Where does the data come from?

 

V

I may not be perfect, but I'm all I got!
0 Kudos
Message 6 of 9
(11,875 Views)

VeeJay,

 

Look closly at the cluster that defines the element type for the queues. There is a 2nd element in the cluster (a variant data type). In the main VI, the Msg string is converted to variant and passed to the Q1 and Q2 queues in the "Send Message to SubVIs" state of the state machine. 

 

Hopefully this clears things up a bit. 

Kyle Hartley
Senior Embedded Software Engineer

0 Kudos
Message 7 of 9
(11,835 Views)

I have several vi's running and collected data from independent sources.  Their data is queued with their respective vi.  Their data is then dequeued in a master application which then combines it into an array.  The problem I am having is that the data dequeued is lagging and slowed substantially compared to the enqueued data.  The collected and then combined data is arraged in a simple while loop with timing minimized however nothing seems to influence the lag and slow loop timing on the dequeued side,...  Anyone have any thoughts

 

Jeff 

0 Kudos
Message 8 of 9
(9,403 Views)

Jeff,

 

You may want to post your code and create a new thread. Is this array preallocated? If you are constantly appending new elements to the array LabVIEW memory manager is having to constantly find new locations for the array. This takes alot of time when you are dealing with large data sets. 

 

If you have more cpu resources you could try having two consumer loops pulling off data and see if that helps, the arrays will end up being a shared resource and may stall out one of the loops.

Kyle Hartley
Senior Embedded Software Engineer

0 Kudos
Message 9 of 9
(9,376 Views)