Question

I need to cache file pointers in my program, but the problem is that I may have multiple threads accessing that file pointer cache. For example, if thread1 asks for a file pointer, and a cache miss occurs, fopen is called and the pointer is cached. Now when thread 2 arrives and cache hit occurs, both the files share the read/write pointer leading to errors. Some things I thought of -

  • I could keep track of when the file is in use, but currently I don't know when it will be released, and including this feature disturbs my design
  • I could send a duplicate of the file pointer in case of a hit, but I don't know any way of doing this so that these two copies do not share read/write locations

How should I proceed?

Was it helpful?

Solution

Are you concerned about optimizing out the file open operation? I think you are making it way more complex and error prone than what it should be. File pointers (FILE*) are not thread-safe structures so you cannot share them across threads.

What you probably need to do (if you really want to cache the file open operations) is to keep a dictionary mapping filename to a file descriptor (an int) and have a thread-safe function to return a descriptor by name or open if it's not in the dictionary.

And of course doing I/O to the same file descriptor from multiple threads needs to be regulated as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top