如何在C ++中写入磁盘的程序中实现并行性? [英] How can I achieve parallelism in a program that is writing to the disk in C++?

查看:128
本文介绍了如何在C ++中写入磁盘的程序中实现并行性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主内存中有一个缓冲区,其中包含一些我想要并行写入(如果可能?)到磁盘的文件。我每次读写不同的位置。

I have a buffer in main memory that contains a couple of files that I want to write in parallel(if possible?) to the disk. I read and write to different location every time.

这是我的代码:

#include <thread>
void t1(){//read from the buffer and writes the 1st file to the disk }
void t2(){//same with the second one }
void t3(){//3rd}
void t4(){//4th}

int main(){
  std::thread thread1(t1);
  std::thread thread2(t2);
  std::thread thread3(t3);
  std::thread thread4(t4);

  t1.join();
  t2.join();
  t3.join();
  t4.join();
}



我知道我可以并行地读缓冲区,是瓶颈。有没有我平行的写到磁盘的方式?还有其他什么,我可以做更好的性能吗?

I know that I can do in parallel the reading of the buffer but the write is the bottleneck. Is there I way that I parallelise the write to the disk? Is there anything else that I can do to have better performance ?

感谢

编辑:到不同的文件。

推荐答案

这很大程度上取决于你要写的数据。

It very much depends on the data you want to write.

写固定大小的数据可以将它分成四个块,每个线程寻找到文件中的特定位置并写入。请注意,您需要四个不同的文件流对象,每个线程一个。

Writing fixed-sized data you could split it up into four chunks, and each thread seeks to a specific position in the file and write there. Note that you need four different file stream objects, one per thread.

写入没有固定大小的数据,如任意文本,是不可能并行进行的。您需要某种类型的同步,因此一次只能有一个线程写入。

Writing data without a fixed size, like arbitrary text, is not possible to do in parallel. You need some kind of synchronization for this so only one thread writes at a time.

此外,即使数据是固定大小的,也可能无法写入

Also, even if the data is a fixed size, it might not be possible to write in parallel, if the data is streaming and can't be split up into chunks.

以上是如果你想要的所有线程写入同一个文件。如果线程每个写入不同的文件,那么没有问题。这与多个进程写入不同文件没有什么不同。

The above is if you want all threads to write to the same file. If the threads each write to different files then it's no problem. That's no different that multiple processes writing to different files.

这篇关于如何在C ++中写入磁盘的程序中实现并行性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆