[fpc-pascal] Delphi compatible anonymous functions

Marco van de Voort marcov at stack.nl
Thu May 22 11:33:35 CEST 2014


In our previous episode, Michael Schnell said:
>   - To through the callback event, create an instance and fill the data 
> structure in the instance and then use TThread.Queue to through the event:
>     procedure TmyThread.Execute....
>     begin
>     ...
>     AsyncEvent := TAsyncEvent.create;
>     AsyncEvent.DataX := .....
>     AsyncEvent.DataY := .....
> Queue(AsyncEvent.DoIt);
> 
>   - now forget the variable AsyncEvent :-) .
> 
>   - some time later the main Thread calls DoIt
>    here we go:
>    procedure DoIt...
>    begin
>      X := DataX;
>      Y := DataY;
>      ... do stuff ...
>      Free;
>    end;

I do something similar, but pool these event records in generic pools, so at
the end of the generic doit (I call it execute) it puts itself back in the pool.
The generic pool is complemented by a generic factory class.

I originally only pooled allocations over 1 MB, but since generics pooling
become easier I pool more readily. (never benchmarked if that matters for
small allocs of course. Big chance that the tthreadlist lock in the pool is
worse than the allocation/free with modern threadaware heapmgrs, since those
probably lock only once to sync multiple deallocations)
 




More information about the fpc-pascal mailing list