Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

COMMON block in DLL

Intel_C_Intel
Employee
1,440 Views
Hello,
This might be a naive question.
I have a DLL that exports only functions. These functions share a COMMOM block in the dll source files.
When a common block is declared in a dll, is this block available to all instances of the DLL loaded by one application ?
I have an windows application that creates multiple threads. Some of these threads load the fortran dll and make some function calls. The problem is on thread modifies the variables in the common block - then is this change visible in all other dll instances ?
thank you.
vikrantca
0 Kudos
6 Replies
Steven_L_Intel1
Employee
1,440 Views
Is this all in one process? If it was, I'd expect that the DLL would be loaded just once and the address space shared. But it's possible that multiple LoadLibrary requests might load it multiple times. It would be useful to add some debugging code to see what handles are returned by LoadLibrary in the various threads and if calls to GetProcAddr in the various threads return the same value for the same routine.

It is possible to create a DLL with a data section that is read-write shared across multiple processes - requires an option when linking.
0 Kudos
Intel_C_Intel
Employee
1,440 Views
So does this mean that when multiple threads from the same process call LoadLibrary they would see the same COMMON section ?
BTW, I tried that and yes, the same handle is returned by LoadLibrary when all the threads are from the same process.
The code that I have uses some shared data in COMMON block amongst functions, but I want this data to separate for each calling thread. I guess modules would be the right choice.
thanks
vikrantca
0 Kudos
Steven_L_Intel1
Employee
1,440 Views
Oh, you WANT the data to be separate! Modules would not help - they would be global for the process too. You may want to consider using OpenMP which provides standardized ways of specifying whether variables are to be shared or not. It's not clear to me why you're loading the DLL in multiple threads, though. That's a process-wide operation.
0 Kudos
Intel_C_Intel
Employee
1,440 Views
I have a wrapper class in c# that loads the fortran dll for some fluid (air, water) calculations. The dll is initialized with a property model (air or water or carbon etc.). This model is stored in the common section.
The different threads of the process use different fluids, thus the common section is overwritten by the most recent initialization call. The initialization is an expensive call. So each instance of the c# class calls initialize only once at the beginning. So the most recent call takes precendence.
Any suggestions you may have would be very helpful.
thank you.
vikrantca
0 Kudos
Steven_L_Intel1
Employee
1,440 Views
I would have to recommend that you don't use global variables of any kind. Declare local variables in the main DLL routine and either have any subroutines be "contained", so that the variables are visible, or pass them around as arguments. Be sure that you add the RECURSIVE keyword to all your routines and link against the multithread version of the Fortran libraries.
0 Kudos
Intel_C_Intel
Employee
1,440 Views
Thanks a lot.
vikrantca
0 Kudos
Reply