[fpc-pascal] How to implement a circular buffer object in pascal?
Martin Frb
lazarus at mfriebe.de
Thu Sep 3 17:44:46 CEST 2020
On 03/09/2020 16:36, Bo Berglund via fpc-pascal wrote:
> A "simplest case" usage example would be nice to have.
True. Once you got enough info from this thread, please write one.
> 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>???
Ok, I see. It's a generic, so this question is not directly related to
the TLazThreadedQueue
type
TMyData = class
FData: array of byte;
end;
TMyThreadedQueue = specialize TLazThreadedQueue<TMyData>;
var
a: TMyData
q: TMyThreadedQueue
q := TMyThreadedQueue.create($8000);
a := TMyData.Create;
q.PushItem(a);
Of course you can do
TMyData = array of byte; // or record .... end if you want
TMyThreadedQueue = specialize TLazThreadedQueue<TMyData>;
or even
TMyThreadedQueue = specialize TLazThreadedQueue<Byte>;
Pushing arrays (dyn ar stat) is fine.
Pushing individual bytes => works. And if you get a byte once or twice
in a second then it is ok.
If you get bytes every few milliseconds or faster, then pushing bytes
may be too expensive (I don't know, but I would expect at some point....).
In order to push (or pop) the queue has to obtain a lock. You have to
make sure your computer can handle that fast enough. (maybe it can....)
>
> And how do you use ShutDown, DoShutDown, QueSize and Grow?
>
> Very confused.
Grow gives a positive/negative delta. It is added to the current size.
But as I said, for this to work you need the patches.
Also "grow" on a live queue, requires to get the lock yourself. This is
not automated. (Might be an idea to add that....)
- If the queue is full then PushItem will wait until timeout, or until
something is popped.
- if the queue is empty PopItem will wait in that manner.
If threads are waiting, and ShutDown is called, then Push/PopItem return
wrAbondoned. (So you need to check for this)
More information about the fpc-pascal
mailing list