[fpc-devel] ref count types / threadsave question

Benito van der Zander benito at benibela.de
Wed Jan 2 21:22:07 CET 2019


but if another core can do anything to the string, the refcount should 
already be 2, one for this core and one for the other core, should it not?

UniqueString has such a test:

Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 
'FPC_ANSISTR_UNIQUE']; compilerproc; inline;
   Make sure reference count of S is 1,
   using copy-on-write semantics.
   pointer(result) := pointer(s);
   If Pointer(S)=Nil then
   if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref<>1 then


Am 02.01.19 um 20:14 schrieb Jonas Maebe:
> On 02/01/19 20:08, Martin wrote:
>> If a local string var has a refcount of 1, then it can not be 
>> accessed by any other thread. Therefore it needs no lock for 
>> decreasing the ref.
> You would need a full memory barrier before checking whether the 
> reference count is one, otherwise it could have been increased to two 
> by another thread on another core quite a while ago without that 
> change being already visible on the current core. This would probably 
> slow down things much more than any potential gain from not performing 
> the atomic decrement.
> Jonas
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190102/d2f0f262/attachment.html>

More information about the fpc-devel mailing list