开发者

Is there a better approach for dealing with mulitple processes reading/writing to a file

开发者 https://www.devze.com 2023-02-12 00:32 出处:网络
I have 3 processes each of which listens to a data feed. All the 3 processes need to read and update t开发者_如何学运维he same file after receiving data. The 3 processes keep running whole day. Obviou

I have 3 processes each of which listens to a data feed. All the 3 processes need to read and update t开发者_如何学运维he same file after receiving data. The 3 processes keep running whole day. Obviously, each process needs to have exclusive read/write lock on the file.

I could use "named mutex" to protect the read/write of the file or I can open the file using FileShare.None.

Would these 2 approaches work? Which one is better?

The programe is written in C# and runs on Windows.


Use a named mutex for this. If you open the file with FileShare.None in one process, the other processes will get an exception thrown when they attempt to open the file, which means you have to deal with waiting and retrying etc. in these processes.


I agree with the named mutex. Waiting / retrying on file access is very tedious and exception-prone, whereas the named mutex solution is very clean and straightforward.


"monitor" is one of you choise since it is the solution of synchronization problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消