如何确保只有一个线程从文件执行 IO?
这是我的用例(使用 C++):我有一个多线程环境,对磁盘上写入的数据结构执行操作。有M个文件。工作流程是:
- 线程从文件读入数据结构
- 对数据结构执行操作
- 将数据结构插入到缓存中 将
- 最近使用的元素写入文件
缓存插入和删除已经是线程安全的。但是,我不知道如何并行化写入和读取,即如果线程 1 正在从文件 1 读取,那么线程 2 可以从文件 2 读取。当然,线程 2 不应该从文件 1 读取。如果我只是插入互斥锁,整个部分被锁定,并且只有一个线程可以同时读取。确保只有一个线程读取一个文件,但同时读取多个文件的最有效方法是什么?
编辑:代码是这样的
for element in elements
file = element.txt
data = file.read()
cache.insert(data)
Here's my use case (using C++): I have a multithreaded environment performing operations on data structures written on disk. There are M files. The workflow is:
- Thread reads from file into a data structure
- Operations on the data structure are performed
- The data structure is inserted in cache
- Last recently used element is written on file
Cache insertions and deletions are thread-safe already. However, I have no idea how to parallelize writes and reads, ie if Thread 1 is reading from File 1, then Thread 2 can read from File 2. Of course Thread 2 should not read from File 1. If I simply insert a mutex, the whole section is locked and only one thread can read at the same time. What is the most efficient way to make sure only one thread reads from one file, but multiple files are read at the same time?
edit: code is something like this
for element in elements
file = element.txt
data = file.read()
cache.insert(data)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将文件名作为键放入 std::map 中。然后添加互斥指针作为值。然后,每当线程有要处理的文件名时,它就会使用互斥锁和锁防护来锁定。
由于操作系统有自己的文件缓存,因此最好使用读锁来让多个线程同时读取文件,并且仍然通过唯一锁来锁定防止写入。
Put file name in an std::map as a key. Then add mutex pointer as a value. Then whenever a thread has a file name to work on, it locks using the mutex and a lock guard.
As OS has its own file cache, it would be good to use read-lock to let multiple threads read a file concurrently and still lock against writing by a unique lock.