简体   繁体   English

多线程写入文件C ++

[英]Multi-threads writing into a file C++

I have a big buffer with integer data (a big integer array) and I want to write it into a file. 我有一个带有整数数据的大缓冲区(一个大整数数组),我想将它写入一个文件。

However I am using openMP and my entire application is now multithreaded. 但是我使用的是openMP,我的整个应用程序现在都是多线程的。 So I wonder, after the buffer is full, is there a way for me to use openMP and have multiple threads writting into the same file making several read-only access to the buffer? 所以我想知道,在缓冲区已满后,有没有办法让我使用openMP并让多个线程写入同一个文件,对缓冲区进行几次只读访问?

If with openMP it is impossible, is it possible to do it any other way? 如果使用openMP是不可能的,是否有可能以其他方式进行? How should I do it? 我该怎么办?

Are there any libraries you know for doing this? 你知道有没有这样的图书馆?

Multiple threads writing to a file will make the process even more slow than it already is, since this will force many seek operations (which are the slowest ones, considering default magnetic HDD's). 写入文件的多个线程将使该过程比现有的更慢,因为这会强制执行许多seek操作(考虑到默认磁性HDD,这是最慢的操作)。

Consider writing the data to the disk with just one thread, but the reading being made with multiples threads accessing directly the in memory array. 考虑只用一个线程将数据写入磁盘,但是使用多个线程直接访问内存数组进行读取。 Once that array is not allocated anymore, the access should be performed at the disk, causing again a big slow down due to the seek times. 一旦不再分配该阵列,就应该在磁盘上执行访问,由于寻道时间导致再次大幅减速。 To make reduce the slow down, you must implement some buffering scheme or use only one thread. 要减少减速,必须实现一些缓冲方案或仅使用一个线程。

There are other things to be considered depending on how are your readings performed and your algorithm implemented, but I believe these are some of the general lines. 还有其他事情要考虑,取决于你的读数如何执行和你的算法实现,但我相信这些是一般的一些。

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

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