If you have a thread which calls a function, AFAIK it’s any global or VCL component calls which make it not thread safe right?
So if your thread called a method like
procedure UpdateRow()
begin
StringGrid1.AddRow(....);
end;
that is not thread safe.
however if you have a method like this
function ParseXML(const XML : String) : string;
var
xml_parser : TXMLParser;
begin
xml_parser := TXMLParser.create;
... do stuff
result := xml_parser.something;
xml_parser.free;
end;
that is thread safe, as long as the TXMLParser is not doing anything unthread safe.
But if two threads call that method at the same time, it wont throw an exception as they both create their own instance of TXMLParser right? They get their own copy. Is that correct?
Hope that makes sense 🙂
Actually, it could be correct but doesn’t have to be. If TXMLParser also uses some global variable that’s not threadsafe, this code would not be threadsafe either! The use of global variables is discouraged but Delphi has quite a few build-in variables that might cause problems when they’re modied and read from different threads.
The unit
xmldom, for example, has two global variables:DefaultDOMVendorandDOMVendors. These aren’t really modified often, thus should be safe to use.The SysUtils unit also has a few global variables used for determining the current locale, date/time formats and whatever more. Those aren’t threadsafe either, although these too are almost never modified.
Even third-party code might use global variables for all kinds of purposes. Some more modern code might even use class variables which would also be a kind of global variables and thus definitely not threadsafe, although the risks are based on the number of times the data is modified…
Without a look in the code of TXmlParser it’s unclear if it’s threadsafe or not. If it uses a third-party DLL like Libxml2.dll then it becomes even more complicated since the handle for this DLL might be stored in a global variable, thus becoming unsafe.
Then again, you said TXmlParser is threadsafe. To answer your question: your code would then be threadsafe too as long as the “Do stuff” part is threadsafe.