在单个应用程序中调用多个线程的dll函数是否安全? [英] Is it safe to call a dll function from multiple threads in a single application?

查看:192
本文介绍了在单个应用程序中调用多个线程的dll函数是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Delphi 2009中编写一个服务器应用程序,它实现了几种类型的身份验证。每个身份验证方法都存储在单独的dll中。第一次使用验证方法,加载相应的dll。该应用程序关闭时才会释放该DLL。



在服务器线程(连接)之间没有任何形式的同步访问dll是否安全?

解决方案

简答:



可能从多个线程调用DLL函数,因为每个线程都有自己的堆栈,并且调用一个DLL函数或多或少与调用自己的代码的任何其他函数一样。



长回答



如果是实际可能取决于使用共享可变状态



例如,如果你这样做:

 DLL_SetUser(UserName,Password)
如果DLL_IsAuthenticated然后
开始
...
结束;

然后,绝对不能使用不同的线程安全。在这个例子中,你不能保证$ code> DLL_SetUser 和 DLL_IsAuthenticated 之间没有其他线程与 DLL_SetUser



但是,如果DLL函数不依赖于某种预定义状态,即所有必要的参数都可以一次使用并且所有其他配置对于所有线程都是一样的,你可以假设它会工作。

 如果DLL_IsAuthenticated(UserName,Password)那么
开始
...
end;

但要小心:DLL函数可能看起来很原子,但内部使用的东西,不是例如,如果DLL创建一个总是具有相同名称的临时文件,或者访问一次只能处理一个请求的数据库,它将被视为共享状态。 (对不起,我想不出更好的例子)



摘要:



如果DLL供应商说,他们的DLL是线程安全的,我会使用它们从多个线程没有锁定。如果他们不是 - 或者即使供应商不知道 - 你应该安全地使用锁定。



至少在遇到性能问题之前。在这种情况下,您可以尝试创建多个包含DLL调用的应用程序/进程,并将其用作代理。


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)?

解决方案

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.

这篇关于在单个应用程序中调用多个线程的dll函数是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆