简体   繁体   English

在多线程环境(C ++)中读写文件的最佳方法

[英]Best way to read/write file in multithreaded environment (C++)

i have a multithreaded program that reads and writes files. 我有一个读取和写入文件的多线程程序。 One thread receives data and writes them in a file. 一个线程接收数据并将其写入文件。 Every 250 Mb of data, a new file is created. 每250 Mb数据将创建一个新文件。 Multiple other threads can read into these files to retrieve data. 多个其他线程可以读入这些文件以检索数据。 I'm using C++ std file stream. 我正在使用C ++ std文件流。

To prevent problems, my current implementation uses two file descriptors for the same file: one for readers and one for the writer. 为防止出现问题,我当前的实现对同一文件使用两个文件描述符:一个用于读取器,一个用于写入器。 A mutex protects from multiple access at the same time, and the file descriptor position is moved each time the mutex owner needs it. 互斥锁可防止同时访问多个文件,并且每次互斥锁所有者需要时,文件描述符位置都会移动。

I really need to be able to read in the file as fast as possible, and the mutex doesn't really help me. 我确实需要能够尽快读取文件,而互斥锁并没有真正帮助我。

Firstly, I would like to know if it's safe to read and write the file or have multiple reads at the same time (on every platform). 首先,我想知道在每个平台上同时读写文件还是多次读取是否安全。 Secondly, if yes, I would like to know how it is safe for the hardware like the "Disk read-and-write head" for a HDD. 其次,如果可以的话,我想知道对于HDD来说,像“磁盘读写头”这样的硬件如何安全? The software works on the disk all the time to save data, and i don't want my algorithm to decrease too much the hard disk life time (already short). 该软件一直在磁盘上工作以保存数据,并且我不希望我的算法减少太多的硬盘寿命(已经很短了)。

Thank you for your help 谢谢您的帮助

There is no problem regarding multiple threads reading the same file. 关于多个线程读取同一文件没有问题。

Now, if I understood your description correctly, you do not modify already-written data, you just continuously append data to your file until it reaches 250Mb, then you continue writing on a new file. 现在,如果我正确理解了您的描述,则不会修改已经写入的数据,只需数据连续追加到文件中,直到达到250Mb,然后继续在新文件上写入即可。

If this is the case, you may not need a mutex at all. 在这种情况下,您可能根本不需要互斥体。 For instance, you might be able to keep your whole "file" into memory until it reaches 250mb, and only then you would write it all to disk, so you know that any files already on disk aren't going to be written anymore and can be read freely with no worries. 例如,您也许可以将整个“文件”保存到内存中,直到达到250mb,然后才将其全部写入磁盘,因此您知道磁盘上已经没有任何文件了,并且可以自由阅读而无后顾之忧。 As for the file that is still being written, you can have a global integer that holds how many bytes (or strings or whatever you use) have already been written, and reading-threads are limited by this integer, which does not need a lock, as long as you only update the integer after you have already written the data. 对于仍在写入的文件,您可以具有一个全局整数,该整数保存已经写入了多少个字节(或字符串或使用的任何内容),并且读取线程受该整数限制,不需要锁定。 ,只要在写入数据后才更新整数即可。 (since you said there is only 1 thread writing data). (因为您说过只有1个线程在写数据)。

Simply reading the integer cannot corrupt it even when being done by multiple threads at the same time and being written by a single one, so this will ensure your reader threads will not read beyond the limit, and such limit will always be safe and consistent, while the writer-thread can peacefully write data in an area that is guaranteed to not be bothered by read-threads until it is finished. 只需读取整数就不会损坏它,即使同时由多个线程完成并由一个线程写入也是如此,因此这将确保您的读取器线程不会超出限制读取,并且该限制始终是安全且一致的,而写程序线程可以在保证完成之前不会被读线程打扰的区域中和平地写入数据。

As for your second question, if you are indeed able to keep the currently-being-written file fully in memory, that will already save up some HDD usage, as well as time. 至于第二个问题,如果您确实能够将当前正在写入的文件完全保留在内存中,那将节省一些HDD的使用以及时间。 Additionally, keep in mind most modern HDDs have 32Mb+ of cache, so it is not like every read and write will be directly hitting the HDD itself, unless you have a ton of threads reading random files and random parts of them all the time. 此外,请记住,大多数现代HDD具有32Mb +的缓存,因此,除非您有大量线程始终读取随机文件及其随机部分,否则并非每次读写都会直接命中HDD本身。 If that is the case, there is probably not much you can do to help the HDD. 如果真是这样,您可能无法做很多事情来帮助HDD。 And if that's not the case, there is not much to worry about, as the OS and the caches will do what they were meant to do :) 如果不是这种情况,就不必担心太多了,因为操作系统和缓存将按原本打算的方式进行:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM