C# 并发文件写入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1160233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:39:00  来源:igfitidea点击:

Concurrent file write

c#loggingconcurrency

提问by pistacchio

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.

就像,如果两个不同的进程在同一时刻写入文件,这可能会导致问题。简单的解决方案(不是很快,也不是很优雅)将在开始进程时锁定文件(创建一个 .lock 文件或类似文件)并在写入完成时释放它(删除锁定)。

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

开始写入时,我会检查 .lock 文件是否存在并延迟写入直到文件被释放。

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

对于这种情况,建议遵循什么模式?

Thanks

谢谢

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

编辑我的意思是进程,比如来自不同客户端、不同用户等的不同程序,而不是同一程序中的线程

回答by Ian

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.

我建议使用 ReaderWriterLock。它是为多个读者设计的,但确保在任何时候只有一个作者可以写入数据MSDN

回答by Broken Link

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

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

回答by joshlrogers

I would look at something like the Command Patternfor 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 ReaderWriterLockto 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.

您还应该使用ReaderWriterLock来确保您可以在写入时从文件中读取。这将是命令模式背后的第二道防线,以便在给定时间只有一个线程可以写入文件。

回答by DanDan

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

考虑使用一个简单的数据库。您将相对容易地获得所有这些内置安全性。

回答by Marcin Deptu?a

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?

在进程之间同步访问的最快方法是使用互斥体/信号量。这个线程回答了如何使用它们,模拟读写锁模式: 是否有一个全局命名的读写锁?

回答by Michael Behan

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

我还建议您查找具有多个读者且关键部分中只有 1 个作者的示例,这是一篇带有很好解决方案的短论文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.

或者,您可以在每次请求时创建文件副本,以及何时将任何更改写入与原始文件合并的文件。