Jump to content

Terminating Producer Consumer Loops


Recommended Posts

Hey everyone

I'm reaching the end of my tether with these while loops running in parallel. My program seems to run fine but then when I hit the stop button it just hangs and doesn't exit. Can someone tell me, or link me to an explanation of how to propagate a stop condition in producer consumer loops, in addition, how should I destroy the queues so that they are destroyed in the right order?

My code is attached if it's of any help at all.

Regards and thanks for your time.

Alex

post-16778-126576501268_thumb.png

Link to comment

Hey everyone

I'm reaching the end of my tether with these while loops running in parallel. My program seems to run fine but then when I hit the stop button it just hangs and doesn't exit. Can someone tell me, or link me to an explanation of how to propagate a stop condition in producer consumer loops, in addition, how should I destroy the queues so that they are destroyed in the right order?

My code is attached if it's of any help at all.

Regards and thanks for your time.

Alex

post-16778-126576501268_thumb.png

The problem is the timeout input on your dequeue node is set to -1 which means it will wait forever until there is an element to dequeue.

One of the primary rules of data flow is any structure (in this case a while loop) must wait until all items are complete before it can continue.

In your program the loop is waiting for the dequeue node to complete and the node is waiting for an element to be put in the queue.

The node will also release if the queue reference is destroyed.

So one solution would be to set the stop buttons and then destroy all of the queues.

This should release the dequeue node and let the while loop continue.

Closing the queue reference will also produce an error out of the dequeue node and many programmers wire the error out to the stop terminal of the while loop.

The Producer/Consumer Design Pattern (Data) uses this method.

post-584-126577991516_thumb.png

Hope this helps

Mark

  • Like 1
Link to comment

Hey Mark

Thanks for your solution, I had a little trouble implementing it initially because I didn't put in the case structure, i.e. if no error -> do code, otherwise -> do nothing. As an aside, how much does having the error checking case slow the code down in your experience? Is an error signal a logic 1 or 0? If so I can't imagine it slowing things down too much, if it's a package containing error information with every check that might slow things down. Your thoughts? I ask because speed is crucial in the application I'm trying to create.

Kind Regards

Alex

Link to comment

And another quick question about queues, if I tell something to enqueue as fast as possible (0 timeout) and my dequeing code can't possibly keep up, what happens to the data? Is it destroyed or lost somehow? Or does the code behave like a lossy enqueue rather than a normal one?

Regards

Alex

Link to comment

Hey Mark

Thanks for your solution, I had a little trouble implementing it initially because I didn't put in the case structure, i.e. if no error -> do code, otherwise -> do nothing. As an aside, how much does having the error checking case slow the code down in your experience? Is an error signal a logic 1 or 0? If so I can't imagine it slowing things down too much, if it's a package containing error information with every check that might slow things down. Your thoughts? I ask because speed is crucial in the application I'm trying to create.

Kind Regards

Alex

Looking at your posted code the error checking case structure is insignificant compared to the other nodes. I doubt you could even measure the difference.

And another quick question about queues, if I tell something to enqueue as fast as possible (0 timeout) and my dequeing code can't possibly keep up, what happens to the data? Is it destroyed or lost somehow? Or does the code behave like a lossy enqueue rather than a normal one?

Regards

Alex

The obtain queue node has a max queue size input. If this is set to -1 the queue can be as big as your RAM will allow.

The data in the queue will only be lost if you release the queue before you dequeue all the items.

If you input a non negative number to the max size then you have two options.

If you use the normal enqueue and the queue is full the node will wait until it has timed out and then discard the input element.

If you use the lossy enqueue the node will push out the oldest element.

post-584-126584529042_thumb.png

two queues.vi

Link to comment

And another quick question about queues, if I tell something to enqueue as fast as possible (0 timeout) and my dequeing code can't possibly keep up, what happens to the data? Is it destroyed or lost somehow? Or does the code behave like a lossy enqueue rather than a normal one?

Regards

Alex

You don't need to specify a 0 timeout if you want to enqueue something as fast as possible. If there is room in the queue, it will be put on it.

If you are using an unbounded queue, then theoretically the function will happily enqueue items until you run out of memory no matter what timeout value you specify.

If you have a bounded queue (as in your example) and it is full, then the enqueue function will wait until the timeout expires or space becomes available. If the timeout happens first, no enqueue takes place. You will not get an error from the function, but you can check the 'timeout' output to see if a timeout occurred.

-Scott

Link to comment

Hey guys

Yeah I reached the conclusion that a 0 timeout on the enqueue is just gonna result in data not getting enqueued at all for that loop iteration, what I was wondering is what happens to that data?

Regards

Alex

To clarify the timeout is the amount of time the enqueue will wait for a spot to become open in the queue.

For queues that have no size limit it's value has no meaning.

a timeout of 0 simply means the node will not wait if the queue is full and will discard the input element.

Mark

Link to comment

To clarify the timeout is the amount of time the enqueue will wait for a spot to become open in the queue.

For queues that have no size limit it's value has no meaning.

a timeout of 0 simply means the node will not wait if the queue is full and will discard the input element.

Mark

I haven't played with the lossy queues but what does the timeout do in that case. Will it wait that long before it queues the data and discards the oldest entry?

Alex,

I would recommend that you use an event structure for capturing your user events. By doing so you will not have to poll the values of the control and your code will be more efficient because your UI task (loop) will be doing nothing unless an event is detected. In a polling model it is constantly doing something and chewing up CPU cycles. (Of course if you only have the base version of LabVIEW you are stuck since NI does not provide the event structure in the base package. Something they really should do.)

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.