c++ fstream并发访问
如果从不同的进程/线程同时访问文件会发生什么? 据我所知,没有锁定文件的标准方法,只有操作系统特定的功能。
就我而言,文件将被经常读取而很少写入。 现在,如果 A
打开一个文件进行读取 (ifstream) 并开始读取块。 B
打开同一个文件进行写入(ofstream)并开始写入。会发生什么?有明确的行为吗?
编辑 我的目标是对许多文件进行并发读取、写入访问。但写访问不会经常发生。如果 fstreams 保证文件内容不会混淆,我会很满意。
例如: 进程1和2写入文件A。如果它们同时写入,我不在乎1或2的版本是否写入光盘,只要它是文件的一致版本即可。
如果一个进程读取一个文件,而另一个进程同时写入该文件,我希望读取进程获取该文件的“旧”版本。
如果 fstreams 不能处理这个问题,我将使用数据库。
What will happen if files are accessed concurrently from different processes/threads?
I understand there is no standard way of locking a file, only os specific functions.
In my case files will be read often and written seldom.
Now if A
open a file for reading (ifstream) and starts reading chunks. And B
opens the same file for writing (ofstream) and starts writing. What will happen? Is there a defined behavior?
edit
My goal is concurrent read, write access to many files. But write access will not occur very often. I would be content if the fstreams guarantee that file content doesn't get mixed up.
E.g.:
Process 1 and 2 write to file A. If they write concurrently I dont't care if the version of 1 or 2 is written to disc, as long as it is a consistent version of the file.
If a process reads a file and another writes to it at the same time, I want the reading process to get the "old" version of the file.
If fstreams don't handle this I will use a database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然没有可移植的方法来使用 C++ 进行有效的文件共享(同时访问)。
您可以使用“锁定”文件共享文件。在打开“foo.dat”之前,尝试创建文件“foo.lock”。继续循环直到成功。访问后,删除foo.lock。这允许串行访问,但不允许并发访问。
您可以以特定于平台的方式使用字节级锁定。 Windows 有 LockFileEx()。 POSIX有fcntl和flock。如果您需要多平台,则需要单独的实现。您可以将它们封装在一个类中,并使用#if 来处理特定于平台的位。
这是最有效(最快)的方法,但它涉及非常复杂的编程并且容易出现错误。
您可以使用 DBMS。
DBMS 是最简单的,但它确实将您与外部产品联系起来,这可能会或可能不会成为问题。按字节锁定比其他任何方法都要快得多,但会增加很多开发和维护成本。
There is certainly no portable way to do efficient file sharing (with simultaneous access) using C++.
You can share files using a "lock" file. Before opening "foo.dat", try to create file "foo.lock". Keep looping until you succeed. After access, delete foo.lock. That allows serial access, but not concurrent access.
You can use byte-level locking in platform-specific ways. Windows has LockFileEx(). POSIX has fcntl and flock. If you need multi-platforms you will need separate implementations. You can encapsulate them in a class and use #if to handle the platform-specific bits.
This is the most efficient (fastest) by a lot, but it involves very complex programming and is prone to bugs.
You can use a DBMS.
A DBMS will be simplest by a lot, but it does tie you to an external product which may or may not be a problem. Byte-wise locking is much faster than anything else, but will add a lot to devel and maintenance costs.
你的目标是什么?您是否试图阻止对文件的并发读/写操作,或者您想通过文件实现某种形式的 IPC?
不管怎样,看看 boost interprocess,它为您提供了使用文件锁(以及 IPC 的其他很酷的东西)的机会,而且它还有可移植的额外优势!
What is your goal? Are you trying to prevent concurrent read/write operations to files or do you want to implement some form of IPC via files?
Either way, look at boost interprocess, it provides you the opportunity to use file locks (and other cool stuff for IPC) And it has the added advantage of being portable!