使用ifstream在两个线程中处理相同的文件 [英] Process same file in two threads using ifstream
问题描述
我的应用程序中有一个包含大量信息的输入文件。按顺序读取它,并且一次只读取一个文件,这对我的应用程序的使用是不够的。理想情况下,我希望有两个线程,它们具有独立且不同的 ifstream
s读取同一文件的两个唯一文件偏移量。我不能只启动一个 ifstream
,然后使用它的复制构造函数复制它(因为它是不可复制的)。 那么,我该如何处理?
I have an input file in my application that contains a vast amount of information. Reading over it sequentially, and at only a single file offset at a time is not sufficient for my application's usage. Ideally, I'd like to have two threads, that have separate and distinct ifstream
s reading from two unique file offsets of the same file. I can't just start one ifstream
up, and then make a copy of it using its copy constructor (since its uncopyable). So, how do I handle this?
我可以立即想到两种方式,
Immediately I can think of two ways,
- 为第二个帖子构建一个新的
ifstream
,在同一个文件中打开它。 - 在两个线程中共享一个打开的
ifstream
的单个实例(例如使用boost :: shared_ptr<>
)。当线程获得时间片时,寻找当前线程当前感兴趣的适当文件偏移量。
- Construct a new
ifstream
for the second thread, open it on the same file. - Share a single instance of an open
ifstream
across both threads (using for instanceboost::shared_ptr<>
). Seek to the appropriate file offset that current thread is currently interested in, when the thread gets a time slice.
是一个这两种方法首选吗?
我还没有想到第三种(或第四种)选项吗?
显然我最终受到硬盘驱动器来回限制的限制,但我感兴趣的是利用(如果可能的话),是一些操作系统级别的磁盘缓存同时在两个文件偏移处。
Obviously I am ultimately limited by the hard drive having to spin back and forth, but what I am interested in taking advantage of (if possible), is some OS level disk caching at both file offsets simultaneously.
谢谢。
推荐答案
两个 std :: ifstream
实例可能是这里的最佳选择。现代硬盘驱动器针对大量I / O请求进行了优化,因此同时从两个 std :: ifstream
实例中读取应该可以提供相当不错的性能。
Two std::ifstream
instances will probably be the best option here. Modern HDDs are optimized for a large queue of I/O requests, so reading from two std::ifstream
instances concurrently should give quite nice performance.
如果你有一个 std :: ifstream
你将不得不担心同步对它的访问,加上它可能会破坏操作系统的自动化顺序访问预读缓存,导致性能较差。
If you have a single std::ifstream
you'll have to worry about synchronizing access to it, plus it might defeat the operating system's automatic sequential access read-ahead caching, resulting in poorer performance.
这篇关于使用ifstream在两个线程中处理相同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!