tellp 与文件锁定一起使用是否安全
我认为boost文件锁定(可共享和范围内的file_locks)和文件锁定的一般策略是这样的:
- 打开
- 锁定
- 对文件内容进行操作
- 解锁
- 关闭文件
但是,我将打开文件进行追加并想要打电话给tellp 看看我在哪里。在上述情况下这样做安全吗?在锁定之前打开文件后,是否会立即设置文件指针,从而可能不受保护?如果是这样,是否有一个标准的习惯用法可以解决这个问题?
The general strategy for boost file locking (sharable and scoped file_locks), and file locking in general I think, is this:
- open
- lock
- operate on file contents
- unlock
- close the file
However, I will be opening the file for append and want to call tellp to see where I am. Is this safe to do in the above scenario? Won't the file pointer be set as soon as the file is opened before the lock and thus potentially not protected? If so, is there a standard idiom to get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是特定于环境的,但在大多数平台上:
当打开文件进行追加时,文件指针会在每次写入之前立即调整。因此,如果您在锁定文件之前使用tellp,它可能不会告诉您新附加的字节将去往何处,但您不应该让两个进程使用锁定以某种方式仍然附加相同范围的字节。字节。
This may be environment-specific, but on the majority of platforms:
When a file is opened for append the file pointer is adjusted immediately before every write. So, if you use
tellp
before locking the file, it might not tell you where your newly appended bytes are going to go, but you shouldn't have two processes using locking somehow still appending the same range of bytes.我推荐好的 boost 文档:
http://www.boost .org/doc/libs/1_45_0/doc/html/interprocess/synchronization_mechanisms.html#interprocess.synchronization_mechanisms.file_lock
其中您可以阅读:
哦,这太糟糕了...我想帮忙,我已经编写了示例代码,请尝试一下...从 1_44 开始,win32 的文件锁定已损坏,刷新对锁定的文件不起作用。
抱歉,不是我的错。
如果有帮助,理论上:如果您打开文件进行追加,则意味着自动查找在每次写入操作之前结束。它不会阻止您随时手动寻求结束 - 即使不写入。然而,经验(见上文)告诉我们:远离破损的东西。
I recommend good boost documentation:
http://www.boost.org/doc/libs/1_45_0/doc/html/interprocess/synchronization_mechanisms.html#interprocess.synchronization_mechanisms.file_lock
In which you can read:
Oh, this is just awful... I wanted to help, I have written sample code, try it and ... as of 1_44 file locking is broken for win32, flushing does not work on locked file.
Sorry, not my fault.
If it helps, in theory: if you open file for appending, it means auto-seeking to end before every write operation. It does not stop you from manually seeking to end anytime you want - even without writing. However, experience (see above) says: stay away from broken things.