Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Thread-Safe version of TryStrToFloat


This question is answered.


Permlink Replies: 2 - Last Post: Feb 14, 2017 10:57 AM Last Post By: Remy Lebeau (Te...
Larry Johnson

Posts: 86
Registered: 6/7/14
Thread-Safe version of TryStrToFloat  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 9, 2017 3:17 AM
I am using XE4 and 32 bit VCL platform.

I just read in the help file the function TryStrToFloat() is only thread safe if you use the local TFormatSettings. I use a lot of threads and never used the TFormatSettings so I have to update my threads. Is the code below the correct way to setup thread-safe versions of TryStrToFloat(), TryStrToInt() and TryStrToInt64()? Thanks.

void __fastcall TMyThreadTwo::Execute()
{
  SetName();
 
  FreeOnTerminate = true;
 
 
  UnicodeString CellValue;
  int MyInt;
  double MyDlb;
  __int64  MyInt64;
 
  // get default settings first...
  TFormatSettings fmt;
  // in XE and later, use this:
  fmt = TFormatSettings::Create(GetThreadLocale());
 
 
  TryStrToFloat(CellValue, MyDlb,   fmt );
  TryStrToInt64(CellValue, MyInt64, fmt );
  TryStrToInt(CellValue,     MyInt,    fmt );
 
}
Ted Lyngmo

Posts: 117
Registered: 10/3/06
Re: Thread-Safe version of TryStrToFloat
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 14, 2017 10:34 AM   in response to: Larry Johnson in response to: Larry Johnson
Larry Johnson wrote:

I just read in the help file the function TryStrToFloat() is only thread safe if you use the local TFormatSettings. I use a lot of threads and never used the TFormatSettings so I have to update my threads. Is the code below the correct way to setup thread-safe versions of TryStrToFloat(), TryStrToInt() and TryStrToInt64()?

That looks like it should work - but unless you change the global formatting in one thread while another thread uses one of the functions (without supplying a format setting), they are all thread safe if I understand it correctly.

Br,
Ted
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Thread-Safe version of TryStrToFloat
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 14, 2017 10:57 AM   in response to: Larry Johnson in response to: Larry Johnson
Larry wrote:

I just read in the help file the function TryStrToFloat() is only
thread safe if you use the local TFormatSettings.

Technically yes. And the same goes for all of the other formatting/scanning
functions that are locale-aware.

I use a lot of threads and never used the TFormatSettings so I
have to update my threads.

Only if the global formatting variables in the SysUtils unit are being modified
at runtime while your threads are running. Otherwise, they are safe to use
as-is in threads without having to use TFormatSettings. But you should get
in the habit of using TFormatSettings in multi-threaded code.

Is the code below the correct way to setup thread-safe versions
of TryStrToFloat(), TryStrToInt() and TryStrToInt64()?

Yes, though technically you can omit the GetThreadLocale() call since TFormatSettings
has a no-parameter Create() that uses the calling thread's current locale:

fmt = TFormatSettings::Create();


TFormatSettings has overloaded Create() methods so you can specify a specific
locale if you actually need to.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02