Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to call a dll function from multiple threads in a single application?

I am writing a server application in Delphi 2009 that implements several types of authentication. Each authentication method is stored in a separate dll. The first time an authentication method is used the appropriate dll is loaded. The dll is only released when the application closes.

Is it safe to access the dlls without any form of synchronisation between the server threads (connections)?

like image 745
norgepaul Avatar asked Apr 11 '09 07:04

norgepaul


1 Answers

Short answer:

Yes, it is generally possible to call a DLL function from multiple threads, since every thread has it's own stack and calling a DLL function is more or less the same as calling any other function of your own code.

Long answer:

If it is actually possible depends on the DLL functions using a shared mutable state or not.

For example if you do something like this:

DLL_SetUser(UserName, Password)
if DLL_IsAuthenticated then
begin
...
end;

Then it is most certainly not safe to be used from different threads. In this example you can't guarantee that between DLL_SetUser and DLL_IsAuthenticated no other thread makes a different call to DLL_SetUser.

But if the DLL functions do not depend on some kind of predefined state, i.e. all necessary parameters are available at once and all other configuration is the same for all threads, you can assume it'll work.

if DLL_IsAuthenticated(UserName, Password) then
begin
...
end;

But be careful: It might be possible that a DLL function looks atomic, but internally uses something, which isn't. If for example if the DLL creates a temporary file with always the same name or it accesses a database which can only handle one request at a time, it counts as a shared state. (Sorry, I couldn't think of better examples)

Summary:

If the DLL vendors say, that their DLLs are thread-safe I'd use them from multiple threads without locking. If they are not - or even if the vendors don't know - you should play it safe and use locking.

At least until you run into performance problems. In that case you could try creating multiple applications/processes which wrap your DLL calls and use them as proxies.

like image 71
Daniel Rikowski Avatar answered Sep 29 '22 11:09

Daniel Rikowski