tellp 与文件锁定一起使用是否安全

发布于 2024-10-11 20:54:23 字数 253 浏览 1 评论 0原文

我认为boost文件锁定(可共享和范围内的file_locks)和文件锁定的一般策略是这样的:

  1. 打开
  2. 锁定
  3. 对文件内容进行操作
  4. 解锁
  5. 关闭文件

但是,我将打开文件进行追加并想要打电话给tellp 看看我在哪里。在上述情况下这样做安全吗?在锁定之前打开文件后,是否会立即设置文件指针,从而可能不受保护?如果是这样,是否有一个标准的习惯用法可以解决这个问题?

The general strategy for boost file locking (sharable and scoped file_locks), and file locking in general I think, is this:

  1. open
  2. lock
  3. operate on file contents
  4. unlock
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟织青萝梦 2024-10-18 20:54:23

这可能是特定于环境的,但在大多数平台上:

当打开文件进行追加时,文件指针会在每次写入之前立即调整。因此,如果您在锁定文件之前使用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.

趁微风不噪 2024-10-18 20:54:23

我推荐好的 boost 文档:
http://www.boost .org/doc/libs/1_45_0/doc/html/interprocess/synchronization_mechanisms.html#interprocess.synchronization_mechanisms.file_lock

其中您可以阅读:

  • 文件锁不是操作系统锁,即使操作系统支持它(例如Windows 是的,类 Unix 通常不是),所以如果你锁定文件,任何人都可以读/写/删除它,除非其他进程使用相同的文件锁定机制。因此,将其视为进程间互斥体而不是真正的文件锁。
  • 文件锁用于进程间同步,它们不会同步进程内的多个线程
  • 不要忘记刷新(ofstream 的刷新),因此您不必担心缓冲

哦,这太糟糕了...我想帮忙,我已经编写了示例代码,请尝试一下...从 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:

  • file locks are not operating system locks, even if operating system supports it (e.g. Windows yes, Unix-like usually no), so if you lock file, anybody can read/write/delete it, unless other process uses same file locking mechanism. Thus think about it more like interprocess mutexes rather than real file locks.
  • file locks are for interprocess synchronization, they do not synchronize multiple threads within process
  • don't forget about flushing (ofstream's flush), so you do not have to worry about buffering

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文