简体   繁体   中英

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 (eg 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?

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM