title: swift: load dylib per class instance (or thread safe)
body: <p>I have a problem involving class instances accessing an external library, which by itself uses global variables and is meant to be accessed as a singleton. Thus, I want/have to load one instance of the dylib for each swift class instance.
  Let me state this in advance: this is my first post here and I apologize, if its quality doesn't match the audience. Feel free to correct me.</p>
  
  <p>The background of the application is the following: I'm developing a framework bundle, which in its turn uses some functions from a dylib installed in the bundle's ./Frameworks folder. The dylib and its dependencies are written in Fortran an compiled with gfortran. I've successfully implemented and tested the c headers for the dylib object, created a module map with them and I'm able to access the fortran functions and use them from within my swift class (passing unsafe pointers all around the place). The schema is as follows:
  myClass.swift -> fortranLib.dylib -> (libgfortran.dylib, libquadmath.dylib)</p>
  
  <p>The issue I'm having is: the implementation of fortranLib.dylib is in a very typical fortran manner - it uses global variables and nested function calls, so it's totally unsafe to call it from more than one instance of myClass. When I have one instance and I initialize the fortran library with its setup_(some pointers) routine, everything works as expected. As soon as I make a second instance of myClass and try to call setup_() and other functions from that second instance weird things start to happen. Which is of course totally logical for me: both swift instances of myClass try to access the same library functions, having no idea of the library's global variables and internal interactions.</p>
  
  <p>My question is: how can I solve this by loading a fresh new fortranLib.dylib for each instance of myClass, as if the instance and the library are independent memory package?</p>
  
  <p>Thank you in advance!</p>