小编Jos*_*man的帖子

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

我想用它boost::interprocess::file_lock来确保x进程写入目录的文件在完成之前P1不会被进程读取P2.要做到这一点,我想在编写P1文件boost::interprocess::file_lock时锁定文件,然后在文件完成时将其解锁.然后P2可以跳过(并返回)任何被锁定的文件.

我遇到的问题是它似乎boost::interprocess::file_lock只允许你锁定存在的文件.但是,如果我首先创建文件,然后锁定它,那么就存在竞争条件:

  1. P1 创建文件
  2. P2 注意到文件并开始阅读
  3. P1 锁定文件
  4. P1 写一些数据
  5. P2读取一些数据,到达终点,最后只得到部分P1输出.

所以我想做的就是创建一个文件,并在创建文件后立即将其锁定.有没有办法使用boost::interprocess::file_lock

c++ boost file-locking interprocess boost-interprocess

6
推荐指数
2
解决办法
2828
查看次数