[fpc-pascal] Strings and Multithreading (case)

Florian Klaempfl florian at freepascal.org
Mon Sep 15 11:14:44 CEST 2008


Nihilist schrieb:
> I thought about usings Objects in Multithreaded objects and had a look 
> at astrings.inc to get some inspiration.
>  
> I had this problem:
>  
> one global string
> two threads
>  
> lets say, both threads set the string to nil. I belive there is such a case:
>  
> Thread 1 checks for S=Nil an fpc_ansistr_decr_ref (svn version, 15.9.)
> Threadswitch to Thread 2
> Thread 2 checks runs the whole procedure, decrements reference count to 
> zero (only one reference (global)) and calls DisposeAnsiString

This is the fault: you are accessing one data location without any 
syncronization

<ansistring>:=nil;

cannot be consider being an atomic operation. Even for ordinals this 
might be not valid:

<longint>:=0;

might not be atomic on an smp if <longint> is not aligned (on x86).



More information about the fpc-pascal mailing list