The full overload of ParseInput (every
parameter explicit, including the dialect) is thread-safe. Each call
works on its own local variables and writes no shared state; the only
shared state it reads is the global FormatSettings (see
below). It can be called from any number of threads at the same time
without synchronization, as long as FormatSettings is not
changed meanwhile.
// Safe from several threads at the same time
R := ParseInput(Input, itDate, '~', [], sopEqual, sqlMySQL);
The other overloads read the global default dialect, set by SetSQLDialect, at each
call. They are safe in several threads as long as
SetSQLDialect is not called once the threads are running.
The usual pattern is to call SetSQLDialect once at startup,
before any other thread is created:
// Main thread, at startup
SetSQLDialect(sqlElevateDB);
// Worker threads: safe, because SetSQLDialect is not called again
R := ParseInput(Input, itDate);
If the dialect must change while threads are running, use the full overload only and give the dialect in each call.
Simultaneous Parse calls on
different TEgStrParser instances
are thread-safe. Each instance keeps its own SqlDialect
and passes it to the full overload of ParseInput; no global
variable is read or written.
// Safe: Parser1 and Parser2 are different instances
// Thread A:
R := Parser1.Parse(Input1, itDate);
// Thread B, at the same time:
R := Parser2.Parse(Input2, itInteger);
Simultaneous calls on the same instance are
not thread-safe: LastResult
and LastControl
are written by every call without synchronization.
When one component must be shared by several threads, protect each call with a lock:
// One lock per shared component
FLock.Enter;
try
R := Parser1.Parse(Input, itDate);
finally
FLock.Leave;
end;
Alternatively, create one TEgStrParser per thread, or
call the full overload of ParseInput in the worker threads
and keep the component for the main thread.
SetSQLDialect and GetSQLDialect access
the global default dialect without synchronization. Do not call
SetSQLDialect while another thread may be calling the
convenience overloads of ParseInput or
GetSQLDialect.
// Unsafe: the dialect may change while the worker thread reads it
// Worker thread:
R := ParseInput(Input, itDate); // reads the default dialect
// Main thread, at the same time:
SetSQLDialect(sqlMySQL); // writes the default dialect
ParseInput reads FormatSettings, the global
Delphi locale settings: it reads them during the call, for the locale
date and time formats, for the separators that RangeSep
must not use and for the display values in Values.
FormatSettings is a global variable shared by all threads;
changing it in one thread while another calls ParseInput is
a data race.
FormatSettings is normally set once at startup and not
changed afterwards, which is safe.
| Scenario | Safe |
|---|---|
ParseInput, full overload,
several threads |
Yes |
ParseInput, other overloads,
several threads, SetSQLDialect only at startup |
Yes |
ParseInput, other overloads,
SetSQLDialect called from another thread meanwhile |
No |
TEgStrParser.Parse, different
instances, several threads |
Yes |
TEgStrParser.Parse, same
instance, several threads |
No |
SetSQLDialect at the same
time as the other overloads or GetSQLDialect |
No |
Changing FormatSettings while
ParseInput runs in another thread |
No |