[fpc-pascal] How to implement a circular buffer object in pascal?
Bo Berglund
bo.berglund at gmail.com
Thu Sep 3 16:36:29 CEST 2020
On Thu, 3 Sep 2020 15:47:53 +0200, Martin Frb via fpc-pascal
<fpc-pascal at lists.freepascal.org> wrote:
>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).
>
A "simplest case" usage example would be nice to have.
I still when looking at the sources have no clue as to how it would be
used...
It seems to me like at the very least one should define what type of
data will be stored in the buffer...
Is this how you use it?
var
MyFifoBuffer: TLazThreadedQueue; 77Whgat do I do about the <T>???
b: byte;
i: integer
begin
MyFifoBuffer := TLazThreadedQueue.Create($8000); //Why no type???
for i:= 0 to 255 do
begin
b := i and 255;
MyFifoBuffer.PushItem(b);
end;
for i := 0 to 255 do
begin
b := MyFifoBuffer.PopItem;
Writeln(IntToHex(b,2);
end;
end.
And how do you use ShutDown, DoShutDown, QueSize and Grow?
Very confused.
--
Bo Berglund
Developer in Sweden
More information about the fpc-pascal
mailing list