I hit on a problem, were an existing code base with excessive multithreading (server) made excessive use of global variables for file handling. But this solution would crash the moment we enabled async executionn in the server application.
This class is just the fundament. In the project itself I don't return the file instance directly, but hidden behind another class that allows me read and write the original file threadsafe.
As such this class only handles the file open and close operations in a thread safe manner. The class garantuees that a file is only opened once. If multiple instances want to access a file they all get the same file instance.
But the class also closes a file instance if nobody wants to access it anymore.