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: 

Could Buffer replace the Queue in Producer/Consumer Design Pattern

Solved!
Go to solution

Hello,

I have a question that the task of Buffer is to store the data and the queue is also of the same so could we use the Buffer inplace of queue in a Producer/Consumer Design Pattern.

0 Kudos
Message 1 of 8
(2,923 Views)

You probably should explain what you mean by "Buffer". Thanks!

0 Kudos
Message 2 of 8
(2,918 Views)

Except for the fact that a Queue kind of IS a buffer........

 

More explanation required.

 

Shane

0 Kudos
Message 3 of 8
(2,915 Views)

Like one used here

0 Kudos
Message 4 of 8
(2,913 Views)

smart buffer example.vi

0 Kudos
Message 5 of 8
(2,911 Views)
Solution
Accepted by topic author Getwonder

No, those buffer examples are not nearly equal to a queue and will never ever "replace" queues in producer/consumer.

The most important advantage of queues for producer/consumer (which none of the other buffer mechanics share) is that it works eventbased to notify the reader that data is available. So if you would simply replace the queue by overly elaborate buffer mechanics as you attached to your last post, you will lose a great deal of the the purpose using producer/consumer.

 

So, to compare both mechanics:

- Queue works eventbased, whereas the buffer example does not.

- Queue has to allocate memory during runtime if more elements are written to the queue than dequeued. This is also true for the buffer (it has to be resized).

- Since the buffer is effectively simply an array with overhead, memory management is getting slow and messy with increasing memory fragmentation. Queues perform way better here (but have their limits there too).

- The overhead for the buffer (array handling) has to be implemented manually. Queue functions encapsulate all necessary functionality you will ever need. So queues do have a simple API, whereas the buffer has not.

- Since the buffer is simply an array, you will have a hard time sharing the content in two parallel running loops. You will need to either implement additional overhead using data value references to manage the buffer or waste lots of memory by using mechanics like variables. In addition to wasting memory, you will presumably run into race conditions so do not even think about this.

 

So this leads to four '+' for the queue and only one point where "buffer" equals the queue.

 

i hope, this clears things up a bit.

Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 6 of 8
(2,903 Views)

Great thanks alot.

0 Kudos
Message 7 of 8
(2,898 Views)

Well said!

Your buffer VIs are actually examples that ship with LabVIEW, however they are ancient! I would guess they pre-date the queue feature by a few versions and are thus quite stale. 😄

0 Kudos
Message 8 of 8
(2,884 Views)