Concurrent file write

2019-02-13 03:10发布

how to write to a text file that can be accessed by multiple sources (possibly in a concurrent way) ensuring that no write operation gets lost?

Like, if two different processes are writing in the same moment to the file, this can lead to problems. The simples solution (not very fast and not very elegant) would be locking the file while beginning the process (create a .lock file or similar) and release it (delete the lock) while the writing is done.

When beginning to write, i would check if the .lock file exists and delay the writing till the file is released.

What is the recommended pattern to follow for this kind of situation?

Thanks

EDIT I mean processes, like different programs from different clients, different users and so on, not threads within the same program

6条回答
贪生不怕死
2楼-- · 2019-02-13 03:35

The fastest way of synchronizing access between processes is to use Mutexes / Semaphores. This thread answers how to use them, to simulate read-writer lock pattern: Is there a global named reader/writer lock?

查看更多
叛逆
3楼-- · 2019-02-13 03:35

I would look at something like the Command Pattern for doing this. Concurrent writing would be a nightmare for data integrity.

Essentially you use this pattern to queue your write commands so that they are done in order of request.

You should also use the ReaderWriterLock to ensure that you can read from the file while writing occurs. This would be a second line of defense behind the command pattern so that only one thread could write to the file at a given time.

查看更多
够拽才男人
4楼-- · 2019-02-13 03:35

You can try lock too. It's easy - "lock ensures that one thread does not enter a critical section while another thread is in the critical section of code. If another thread attempts to enter a locked code, it will wait (block) until the object is released."

http://msdn.microsoft.com/en-us/library/c5kehkcz%28VS.71%29.aspx

查看更多
Luminary・发光体
5楼-- · 2019-02-13 03:48

I would also recommend you look for examples of having multiple readers and only 1 writer in a critical section heres a short paper with a good solution http://arxiv.org/PS_cache/cs/pdf/0303/0303005v1.pdf

Alternatively you could look at creating copies of the file each time it is requested and when it comes time to write any changes to the file you merge with the original file.

查看更多
爷的心禁止访问
6楼-- · 2019-02-13 03:51

Consider using a simple database. You will get all this built-in safety relatively easy.

查看更多
SAY GOODBYE
7楼-- · 2019-02-13 03:51

I suggest using the ReaderWriterLock. It's designed for multiple readers but ensures only a single writer can writer data at any one time MSDN.

查看更多
登录 后发表回答