有没有办法以原子方式从文件C++中读取一行

tuc*_*ber 6 c++ parallel-processing multithreading fstream atomic

我目前正在开发一个项目,我有一个大文本文件(15+ GB),我正在尝试在文件的每一行上运行一个函数.为了加快任务速度,我创建了4个线程并试图让它们同时读取文件.这与我的相似:

#include <stdio.h>
#include <string>
#include <iostream>
#include <stdlib.h> 
#include <thread>
#include <fstream>

void simpleFunction(*wordlist){
    string word;
    getline(*wordlist, word);
    cout << word << endl;
}
int main(){
    int max_concurrant_threads = 4;
    ifstream wordlist("filename.txt");
    thread all_threads[max_concurrant_threads];

    for(int i = 0; i < max_concurrant_threads; i++){
        all_threads[i] = thread(simpleFunction,&wordlist);
    }

    for (int i = 0; i < max_concurrant_threads; ++i) {
        all_threads[i].join();
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

getline函数(以及"*wordlist >> word")似乎会增加指针并以2个步骤读取值,因为我会经常得到:

Item1
Item2
Item3
Item2
Run Code Online (Sandbox Code Playgroud)

背部.

所以我想知道是否有办法原子地读取文件的一行?首先将它加载到数组中是行不通的,因为文件太大了,我不希望一次加载文件.

我遗憾地找不到关于fstream和getline原子性的任何内容.如果有一个原子版的readline甚至是一个简单的方法来使用锁来实现我想要的东西,我全都耳朵.

提前致谢!

vll*_*vll 5

执行此操作的正确方法是锁定文件,这将阻止所有其他进程使用它。请参阅维基百科:文件锁定。这对您来说可能太慢了,因为您一次只读取一行。但是,如果您在每个函数调用期间阅读 1000 或 10000 行,那么这可能是实现它的最佳方法。

如果没有其他进程访问该文件,并且其他线程不访问该文件就足够了,则可以使用访问该文件时锁定的互斥体。

void simpleFunction(*wordlist){
    static std::mutex io_mutex;
    string word;
    {
        std::lock_guard<std::mutex> lock(io_mutex);
        getline(*wordlist, word);
    }
    cout << word << endl;
}
Run Code Online (Sandbox Code Playgroud)

实现程序的另一种方法可能是创建一个始终将行读取到内存的单个线程,而其他线程将从存储它们的类请求单行。你需要这样的东西:

class FileReader {
public:
    // This runs in its own thread
    void readingLoop() {
        // read lines to storage, unless there are too many lines already
    }

    // This is called by other threads
    std::string getline() {
        std::lock_guard<std::mutex> lock(storageMutex);
        // return line from storage, and delete it
    }
private:
    std::mutex storageMutex;
    std::deque<std::string> storage;
};
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的帮助!我在第一个示例中使用互斥体进行了测试,因为它更容易快速实现。它正确读取了文件,并从 1 核到 2 核提供了明显的加速,但之后就趋于平缓。我想来自 3 个以上线程的锁会减慢速度。我想第二个结果将更具可扩展性,我将在稍后实施。再次感谢! (2认同)