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: 

Learning to use queues producer consumer event

Solved!
Go to solution

Hi all,

 

I'm trying to understand good programming techniques for using queue elements in a producer / consumer event driven program. I'm searched for some demo programs to get a basic feel on how these looops work. Currently, I'm unsure on how to deal with errors that occur before an enqueue event (see screen capture below).

 

If an enqueue receives an error in, it looks like the queue element is never received. I have an OR statement to stop the producer loop upon receiving an error message. However, because the item was never enqueued, the dequeue element in the consumer loop just hangs so the consumer loop never closes the while loop. 

 

What are some good techniques for handling errors that get passed into an enqueue? I could probably wire a timeout on the dequeue, but I'd prefer not to use this approach if possible.

 

Thanks
0 Kudos
Message 1 of 13
(3,973 Views)

Place the release queue after the producer loop. It executes even if an error comes in. The consumer loop will throw error 1122 or 1 at the dequeue function, which you can cancle with the general error handler.

 

Edit:

great resource is this blog entry on expressionflow

You also might want to look at my advanced discussion on an event based bus architecture in this  community nugget

 

Felix

Message Edited by F. Schubert on 04-29-2010 01:41 PM
0 Kudos
Message 2 of 13
(3,969 Views)

 


F. Schubert wrote:

Place the release queue after the producer loop.


 

 

I will agree...and disagree. There are two sides to the coin.

 

1. Some will say the creater should be the destroyer.

2. Others will say, well what about if the consumer loop is not done consuming? If the producer quits and destroys the queue, there could be necessary operations left undone in the consumer loop. For instance, what if when you hit quit, the producer queues up "go to exit state" and then the queue is destroyed before the consumer dequeues that message? You never go to your exit state.

 

It all depends on your architecture I suppose, and like i said it can be argued either way.

 

 

 

Message Edited by for(imstuck) on 04-29-2010 02:22 PM
0 Kudos
Message 3 of 13
(3,948 Views)

What about having the producer queue up a stop command or stop flag.  When the consumer sees it, it ends.  To me this is way better than letting an error stop the consumer loop.  The producer will stop producing after the stop condition.  The consumer will have time to consume all queued data before it ends.

 

- tbob

Inventor of the WORM Global
0 Kudos
Message 4 of 13
(3,934 Views)

I also prefer the technique described by tbob.  For some systems I have pair of queues between the loops.  The consumer to producer queue carries status messages, including "stopped," which lets the producer know when it is appropriate to shut down.  Before the event structure I always used this type of two way communications between the loops.

 

Lynn 

0 Kudos
Message 5 of 13
(3,927 Views)
tbob, thanks for that explanation. I think it makes sense. Please correct me if I am misunderstanding the implementation in any way. I've changed the program to allow the consumer to release the queue. I've added the error handler to the end of the producer loop (it runs as soon as the producer loop stops due to error). It seems to be working ok so far.
0 Kudos
Message 6 of 13
(3,921 Views)

It would be better if you attached your vi instead of a picture so I can look at all your cases.  Your error reporting should be in the Error case, not in the No Error case.  It doesn't seem that you implemented the stop command as I have stated before.

 

- tbob

Inventor of the WORM Global
0 Kudos
Message 7 of 13
(3,908 Views)
Here is a template of the program I am using.
0 Kudos
Message 8 of 13
(3,899 Views)
Also, what is the best way of shutting down the producer if an error is only generated in the consumer loop? The error in the consumer shift register won't be passed back to the shift register of the producer loop. Though the consumer will release the queue casuing an error to generate in the producer if a queue element is used. But what if an event triggers in the producer loop that doesn't use a queue element? The error in will appear to be ok.
0 Kudos
Message 9 of 13
(3,892 Views)

I have made slight changes to your vi.  If there is an error in the producer loop, the error dialog will be displayed with a producer error message.  If there is an error in the consumer loop, a consumer error will be displayed.  However, a consumer error will also cause a producer error since the queue will be terminated, so expect to see a producer error after the consumer error.  But I think this is OK, just disregard the error since you know it was caused by the consumer.  There is no good way to shut down the producer if a consumer error occurs.  Just let the terminated queue shut it down.  This is OK.

 

If you have events in the producer that does not queue up anything, the consumer has no way of knowing it.  This is nothing to be concerned with if you don't expect any consumer action.  A consumer error will still shut down the producer loop.

 

 

- tbob

Inventor of the WORM Global
0 Kudos
Message 10 of 13
(3,880 Views)