[fpc-pascal] Thread Safety of String

md at rpzdesign.com md at rpzdesign.com
Wed Jun 4 18:56:27 CEST 2014


Hello:

Is a string[30] declaration under Linux thread safe?

I am not worried about atomicity, just that the memory for a string[30]
if thread safe.

For UTF8String, it is not.

I have 2 threads which are comparing/changing memory and I do not
want criticalsections wrapping the memory accesses for speed reasons.

Type
  Class TTest(TObject)
  public
    nflag : integer ;
    sflag : string[30]
  end;


Gui Thread

gtest := TTest.Create ;

Thread #1 - Constantly changing values

while true do
begin
 gtest.nflag := Random($FFFFFFFF) ;
 gtest.sflag := inttostr(gtest.nflag) ;
end;

Thread #2 - Constantly Comparing values

While true do
begin

  if gtest.nflag = 777888 then
  begin
    break ;
  end;

  //Test string matching instead of integer matching
  if gtest.sflag = '123456' then
  begin
    LaunchSpaceShuttle(gtest) ;
  end;

end;


Thanks for any replies,

marco



More information about the fpc-pascal mailing list