Module Variable Scope

Module Variable Scope

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!

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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.

Yolanda Chen Intel Developer Support Tools Knowledge Base: http://software.intel.com/en-us/articles/tools

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

Steve Intel Developer Support

Login to leave a comment.