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

Module Variable Scope

Aaron_S_1
Beginner
340 Views

If I have a DLL with module variables, is it safe to call functions that use those variables in a parallel process? I want to avoid passing the debug level and output file to all of my functions in a DLL. So instead, my first function call into the DLL initializes 2 private module variables in the module logging: output_file and debug_level. Then I define a subroutine write_to_log( message ). In a non-parallel application, from anywhere in the dll I can say "use logging" and simply write "call write_to_log( some_message )." If I have multiple threads using this library, will all my logging messages go to the output file initialized by the last logging initialization call? My understanding is that they will not, but they would if I declared the variables in a common block. Is this correct?

If this question has been asked before and I missed it, or if there are any references out there please point me in that direction. Thanks for your time!

0 Kudos
2 Replies
Yuan_C_Intel
Employee
340 Views

Hi, Aaron

Did you use static local variable in your write_to_log function? I have to say both the static local veriable and common block are unsafe in mult-threading application. Because they all can be accessed by different threads at the same time. To use those shared variables, you must synchronize them before access. There are many ways to synchronize objects depending on how you multi-threads your code.

Here is an article on threading fortran applications. You can have a look at the section: Thread Safety for detail:

https://software.intel.com/en-us/articles/threading-fortran-applications-for-parallel-performance-on-multi-core-systems

Here is also a similar discussion for your referenece:

https://software.intel.com/en-us/forums/topic/369135

Thank you.

0 Kudos
Steven_L_Intel1
Employee
340 Views

Module variables are also static, like COMMON, and need to be protected if used in a multithreaded environment.

0 Kudos
Reply