I have a multithrred program that reads and writes files and receives a thread data and writes them in a file. Every 250 MB data is created, a new file is created. Many other threads can be read in these files to get data. I am using the C + + STD file stream.
To prevent problems, my current implementation uses two file descriptors for the same file: one mute for the reader is saved from multiple access at the same time for another author, and Every time a mute x owner needs it.
I need to be able to read the file as much as possible, and the Mute X does not really help me.
First of all, I would like to know whether this file is safe to read and write or reads multiple (on each platform) at the same time. Secondly, if yes, then I would like to know how it is safe for hardware like "disk read-and-wright head" for hard disk. I works on disk all the time to save data, and I The algorithm does not want to reduce too much hard disk Lifetime (already small).
Thank you for your help
About the many threads reading the same file There is no problem in.
Now, if I understand that your statement is not correct, you do not modify pre-written data, then you only consistently attach does not give data to your file until it reaches 250 MB, then you keep typing on a new file. / P> If this is the case, then you may not need a mute at all. For example, you might be able to keep your entire "file" in memory until it reaches 250 MB, and then you can write it to disk, so that you know No files are already being written to the disc and can be freely read with no worries. For the file that is still being written, you can have a global integer in which how many bytes (or strings) Y Whatever you use) has already been written, and reading threads are limited by this integer, for which lock is not required, unless you update the integer only after writing data ( Since you have said that only 1 thread is written data). The bus can not be corrupted by reading the whole, when several threads are being written at the same time and written by a single, then your reader threads will not be read beyond the boundaries, And such a range will always be safe and consistent, while author-thread can write peaceful data in a region that is guaranteed not to be disturbed by the reading thread unless it ends. For your second question, if you are actually able to keep the file currently written in full memory, then it can already save some HDD usage, as well as save time. In addition, keep in mind that most modern HDDs have 32 MB + cache, so it is not that every reading and writing will kill directly HDD, as long as you have tons of tons to load random files and all the time There are no random parts. If that is the case, then you probably can not do much to assist HDD. And if it is not, there is nothing more to worry about the OS, and it will cache what they were done to do :)
No comments:
Post a Comment