并发文件写入 [英] Concurrent file write

查看:101
本文介绍了并发文件写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何写入可以由多个来源(可能以并发方式)访问的文本文件,确保没有写入操作丢失?



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



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



什么是



EDIT


我的意思是过程,像来自不同客户端的不同程序,不同用户等,不是同一程序中的线程

解决方案

在进程之间同步访问的最快方法是使用Mutexes / Semaphores。这个线程回答如何使用它们,模拟读写锁定模式:
是否有全局命名的读写器锁定?


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

解决方案

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?

这篇关于并发文件写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆