加载DLL多次,让多线程在.net多线程、加载、DLL、net

2023-09-03 01:44:45 作者:你纵容的任性。

我的.Net程序使用一个Fortran DLL来执行数学函数(ARPACK,解决了固有的模式)。我相信FORTRAN包含静态变量下,通常不是线程安全的。此外,它是非常复杂的,可能会需要很多艰苦的工作,使其线程安全的。该DLL不是很大(700K),所以我只是想加载了很多次(比如4,或者8),以使线程同时工作。任何人有任何想法如何,我可以做到这一点?我听说,当调用多次调用LoadLibrary将始终返回相同的句柄。所以,因为它代表我唯一的解决办法是有我的DLL的多个副本在磁盘上(Arpack1.dll,Arpack2.dll等),并根据需要加载它们。 pretty的可怕。

My .Net program uses a fortran Dll to perform a maths function (Arpack, solves eigen modes). I believe the fortran contains static varibles and generally isn't thread safe. Also it's very complicated and would probably take a lot of hard work to make it thread safe. The Dll isn't very large (700K) so I just want to load it many times (say 4, or maybe 8) to allow the threads to work concurrently. Anyone have any idea how I can do this? I hear that LoadLibrary will always return the same handle when called multiple times. So, as it stands my only solution is to have multiple copies of my Dll on disk (Arpack1.dll, Arpack2.dll etc) and load them as required. Pretty horrible.

任何想法?

尤安

推荐答案

您找到解决方法实际上是pretty的体面之一。可能有小几率LoadLibraryEx()与LOAD_LIBRARY_AS_IMAGE_RESOURCE选项将工作。该选项允许你加载它多次。我认真虽然怀疑这一点,该DLL几乎肯定依赖于获取其运行时支持code到DllMain中初始化。

The workaround you found is actually a pretty decent one. There might be small odds that LoadLibraryEx() with the LOAD_LIBRARY_AS_IMAGE_RESOURCE option will work. That option allows you to load it multiple times. I seriously doubt it though, the DLL almost certainly relies on getting its runtime support code initialized through DllMain.

有一件事我没有听说你提到的是不必使用GetProcAddress的()的疼痛。请确保你做什么或当您启动线程,你还是会踩全局变量。每个线程必须使用它自己的地址。

One thing I didn't hear you mention is the pain of having to use GetProcAddress(). Make sure you do or you'll still stomp global variables when you start threading. Each thread must use its own address.