[fpc-pascal] How to implement a circular buffer object in pascal?

Martin Frb lazarus at mfriebe.de
Thu Sep 3 15:47:53 CEST 2020


On 03/09/2020 14:54, Bo Berglund via fpc-pascal wrote:
> Now to my question:
> Is there some *example* around for using TLazThreadedQueue as a
> circular buffer?
> In the examples dir are only examples for LazUnicode and
> LookUpStringList...
> And when reading the sources I cannot really say I understand what it
> does or how it works.
>
> Is it even applicable for storing data such as a sequence of bytes
> arriving via a serial port?
> The names PushItem and PopItem really suggest it works like a LIFO
> buffer rather than a FIFO buffer, which is what I need...
>
> LIFO: Last In First Out, like how a stack works
> FIFO: First In First Out, like a tube where the items reside for a
> while but come out in orderly sequence.
>

It is FIFO. Its a queue, not a stack.

Look at my latest commits in Trunk to fpdebug > FpDbgUtil
I use the queue there.

Note:
- Calling "grow" when the buffer is NOT empty, works in latest trunk only.
Actually you want that fix anyway, because on Win there is a race 
condition, that could starve individual threads.
r 63811 and 63833 (must take both / I introduced a silly type in the first)

- Calling shutdown (to stop all threads from waiting), may also not 
work, if there are many threads (at least on win). But the fix is in 
trunk, and easy to port. r 63860

You need a type (e.g. class, or array, or pointer) that you can then push.
So if you get bytes from an input, I suggest you push collected chunks 
of them (fixed or dynamic size).




More information about the fpc-pascal mailing list