[fpc-pascal] Is TFPList thread safe?

Dennis Poon dennis at avidsoft.com.hk
Mon Oct 6 17:55:54 CEST 2014


Michael Schnell wrote:
> On 10/02/2014 03:59 PM, Xiangrong Fang wrote:
>> , not replace TFPList with TThreadList, for simplicity and 
>> performance reason...
> I don't suppose it will be possible to beat TThreadList performance by 
> any home-brew Pascal code, which is same is not insecure.
>
But I looked at the source code of TThreadList,

procedure TThreadList.Add(Item: Pointer);
   begin
     LockList;
     try
       if (Duplicates=dupAccept) or
         // make sure it's not already in the list
         (FList.IndexOf(Item)=-1) then
          FList.Add(Item)
        else if (Duplicates=dupError) then
          FList.Error(SDuplicateItem,PtrUInt(Item));
     finally
       UnlockList;
     end;
   end;


where

function TThreadList.LockList: TList;
   begin
     Result:=FList;
     System.EnterCriticalSection(FLock);
   end;


There is no special optimization at all.  It simply calls
   System.EnterCriticalSection(FLock),
so performance should be JUST THE SAME as wrapping a pair of critical 
section enter/leave around the code ourselves.

Dennis






More information about the fpc-pascal mailing list