multithreaded access to win32 files

Started by
2 comments, last by Evil Steve 15 years, 3 months ago
I am in the process of writing a VFS, but came across a problem when considering that I will be using threads in other areas of the code, and the VFS will read archives as separate files. Assuming that the actual access to the files is hidden behind an interface, how would I go about making access to files in the same archive thread safe? I'm writing in C++, using win32 functions. Any help would be much appreciated.
Advertisement
Quote:Original post by webwraith

and the VFS will read archives as separate files.


Can the VFS contents and structures change while application is running? Can files be modified/added/removed one the VFS is fully loaded by application?

If no, then all you need to do is to make sure you don't use globals, and you're thread-safe.

If yes, then the problem depends on how reliable you want to make the library. This involves the file system and database domains and is generally non-trivial if scalability is a concern.

If scalability is not an issue, but file system needs to be mutable, then simply wrap every function that touches your file system with a lock/mutex/critical section/synchronization primitive of choice.

OS file access functions however are thread-safe from implementation perspective, and provide rudimentary protection for concurrent changes of underlying file system.
I re-read my post, and decided that "read archives as separate files" should be written; "treats the archive as a directory"

The VFS will load an archive when the user wants to load a file from it. The problem comes when different threads both want to read files stored in the same archive. I hadn't thought of writing to files at this point, only reading them, but I will look at it as a requirement.

EDIT: OK, so my current list of requirements is;
1. Read from archives as though they were top-level directories
2. Provide multiple root paths for file access, the default being "./"
3. Create new files, and write to them. Editable files are placed in the write_path, which, by default, is "./save/"
4. Read from files as separate files, hiding whether they are in an archive or directory

My question about multiple access to a single archive stands, however. If there isn't a way around this, I may have to rethink the structure of the file access code, which won't really be a problem, I'd just like to know if there is a way to do this safely or not.
Quote:Original post by webwraith
I re-read my post, and decided that "read archives as separate files" should be written; "treats the archive as a directory"

The VFS will load an archive when the user wants to load a file from it. The problem comes when different threads both want to read files stored in the same archive. I hadn't thought of writing to files at this point, only reading them, but I will look at it as a requirement
There's no problem with multiple threads reading the same data; the problem comes when one or more needs to write to it, because then the reading thread could be reading the old data or the new data (or a mix).
You can have 1000 threads reading the same byte of memory at the same time without any problems if you want.

This topic is closed to new replies.

Advertisement