[fpc-pascal] Strings and Multithreading, possible Solution.

Nihilist kccmp at gmx.de
Mon Sep 15 00:43:55 CEST 2008


A possible Solution to the potential error in astrings might be to use interlockedexchange (for pointers)

Attempt:

Procedure fpc_AnsiStr_Assign (Var DestS : Pointer;S2 : Pointer);[Public,Alias:'FPC_ANSISTR_ASSIGN'];  compilerproc;
{
  Assigns S2 to S1 (S1:=S2), taking in account reference counts.
}
begin
  if DestS=S2 then
    exit;
  If S2<>nil then
    If PAnsiRec(S2-FirstOff)^.Ref>0 then
      inclocked(PAnsiRec(S2-FirstOff)^.ref);
  { Decrease the reference count on the old S1 }

// Changed part
  S2:=InterLockedExchangePointer(DestS,S2); 
  fpc_ansistr_decr_ref (S2);

  { And finally, have DestS pointing to S2 (or its copy) }
// Removed   DestS:=S2;
end;

That would give us a "Nil" the second time, which would be ignored by decr_ref then.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20080915/5b9ae556/attachment.html>


More information about the fpc-pascal mailing list