rossmcm
rossmcm

Reputation: 5630

Delphi 2007, Indy - Is a variable declared in a TidTCPServer thread safe in contexts?

If I have a TidTCPServer instance and I declare a TFormatSettings and populate it in the Create routine, is it safe to reference this variable (e.g. call Format ('%1.6f', [SomeReal], AFormatSettings]) in the thread's Execute method, when there might be more than one context executing?

If not, how might I make thread-safe references?

Upvotes: 1

Views: 174

Answers (2)

Mike Taylor
Mike Taylor

Reputation: 2524

If you are ever in doubt about thread safety, you could create the following function and use it in place of Format.

ThdSafeFormat(const aFormat: string; const aArgs: array of const): string;
var
  FormatSettings: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_USER_DEFAULT, FormatSettings);
  Result := Format(aFormat, aArgs, FormatSettings);
end;

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 597036

It is thread-safe as long as you are modifying AFormatSettings only when no threads are accessing it (such as initializing it before activating the server), and the threads are only reading from it. Format() does not modify the TFormatSettings that is passed to it.

Upvotes: 3

Related Questions