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:

Here is also a similar discussion for your referenece:

Thank you.

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

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today