用C ++异步读取文件

时间:2015-03-01 22:11:07

标签: c++ thread-safety queue c++14

我有一些文件大于单核心可用的内存。因此,文件本身不能一次完全读取。该文件分为几个部分("记录"),按顺序使用。

所以我对这样的事情感兴趣:

struct GetFile {
    GetFile(string filename, size_t num_records) 
       : filename(filename), num_records(num_records) {}

    unique_ptr<vector<double>> get_next_record() {
    // This should always return a new record, 
    // and throw if there isn't one.  But this 
    // shouldn't FETCH a new record.
        return queued.pop_front();
    }

private:
    queue< unique_ptr<vector<double>>, 
           deque<unique_ptr<vector<double>>> > queued;
}

但是在这里的某个地方,需要有一个条件变量和另一个线程来在队列变低时填充队列。这里有最好的做法吗?我想将此保留为C ++ 14代码,但如果有库存这样做的话,我很好奇他们是什么。

unique_ptr在这里是正确的吗?看起来这是消费载体的一种好方法,所以当它们超出范围时它们会消失,但我不是肯定的,它们是必要的。

1 个答案:

答案 0 :(得分:0)

这个问题似乎不是异步的&#39;读一个文件, 它更像是阅读一个大文件。考虑相应地编辑标题?

从您的代码中,您似乎不需要随机访问您的数据进行处理。如果顺序处理确实足够,请保持简单:

    std::vector<double> get_next_record(std::ifstream& file) {
        // Read a chunk of data, e.g. 1000000 values and return them
    }

    void process(std::ifstream& file) {
        while (file) {
            // Notice: No need for unique_ptr<> or similar
            std::vector<double> chunk = get_next_record(file);
        }
    }

如果您 需要随机访问您的数据,您需要使用 某种内存映射的文件。检查内存映射 在这种情况下,boost进程库中的文件:

http://www.boost.org/doc/libs/1_55_0/doc/html/interprocess.html

请注意,无需使用unique_ptr&lt;&gt;为了 从函数返回向量。 RVO负责: http://en.wikipedia.org/wiki/Return_value_optimization

相关问题