简体   繁体   English

使用boost :: interprocess :: file_lock创建一个锁定的文件

[英]Create a locked file with boost::interprocess::file_lock

I'd like to use boost::interprocess::file_lock to ensure that files that are written to a directory x by process P1 are not read by process P2 until they are complete. 我想使用boost::interprocess::file_lock来确保进程P1写入目录x的文件在完成之前不被进程P2读取。 To do this, I'd like to have P1 lock the files with boost::interprocess::file_lock while it's writing them, and then unlock them when it's done. 要做到这一点,我想让P1在写入文件时用boost::interprocess::file_lock锁定文件,然后在完成后解锁它们。 Then P2 can just skip over (and come back to) any files that are locked. 然后P2可以跳过(并返回)任何被锁定的文件。

The problem I'm having is that it appears that boost::interprocess::file_lock only lets you lock files that exist. 我遇到的问题是看起来boost::interprocess::file_lock只允许你锁定存在的文件。 But if I first create the file, and then lock it, then there's a race condition where: 但是,如果我首先创建文件,然后锁定它,那么就存在竞争条件:

  1. P1 creates the file P1创建文件
  2. P2 notices the file and starts reading it P2注意到文件并开始阅读
  3. P1 locks the file P1锁定文件
  4. P1 writes some data P1写了一些数据
  5. P2 reads some data, gets to the end, and ends up with only part of P1 's output. P2读取一些数据,到达终点,最后只有P1的一部分输出。

So what I'd like to do is create a file and have it be locked as soon as it's created. 所以我想做的就是创建一个文件,并在创建文件后立即将其锁定。 Is there a way to do this using boost::interprocess::file_lock ? 有没有办法使用boost::interprocess::file_lock来做到这boost::interprocess::file_lock

You misunderstand the purpose of boost::interprocess::file_lock, when you create a file_lock using method boost::interprocess::file_lock test_lock("my_file"), you are not protecting the file "my_file" from reading/writing 你误解了boost :: interprocess :: file_lock的目的,当你使用方法boost :: interprocess :: file_lock test_lock(“my_file”)创建一个file_lock时,你没有保护文件“my_file”不被读/写
by other processes, you just declare that you have a lock that references to the file "my_file", if other processes also have locks that reference to the same file, you can implement mutual exclusion between these locks, but these locks don't care about the read/write operation on the file "my_file", the file is just a flag 通过其他进程,您只需声明您有一个引用文件“my_file”的锁,如果其他进程也有引用同一文件的锁,您可以在这些锁之间实现互斥,但这些锁并不关心关于文件“my_file”的读/写操作,该文件只是一个标志

No. But there is a workaround that only uses one extra empty file. 不。但是有一种解决方法只使用一个额外的空文件。

Before P2 ever tries to scan for files, create an empty file with a name that is well known to both P1 and P2. 在P2尝试扫描文件之前,创建一个名为P1和P2众所周知的空文件。 Before P2 starts scanning it will lock that empty file and release the lock when it is done scanning the directory (ie it shouldn't hold the lock while reading in data from files). 在P2开始扫描之前,它将锁定该空文件并在扫描目录时释放锁定(即,在从文件读取数据时不应保持锁定)。 Before P1 creates a new file it will lock that empty file and release the lock after the new file has been created and locked. 在P1创建新文件之前,它将锁定该空文件,并在创建锁定新文件后释放锁定。

I think how you should be able to avoid the race condition is as follows: 我认为你应该如何避免竞争条件如下:

  1. P1 creates the file P1创建文件
  2. P2 notices the file: a. P2注意到文件:a。 Locks it and b. 锁定它和b。 Starts reading it 开始阅读它
  3. P1 tries to get a lock on the file, has to wait. P1试图锁定文件,必须等待。
  4. P2 done reading, unlocks the file P2完成阅读,解锁文件
  5. P1 locks the file P1锁定文件
  6. P1 writes some data P1写了一些数据

Let me know if that is not clear. 如果不清楚,请告诉我。

Thanks, 谢谢,

Mohit 莫希特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM