简体   繁体   English

C# - 安全多线程保存文件访问

[英]C# — Safe Multiple Thread Save File Access

I am currently developing a C# application that uses two separate threads , the first is the main thread which the program operates on and the second is the log thread . 我目前正在开发一个使用两个独立线程C#应用程序 ,第一个是程序操作的主线程 ,第二个是日志线程 The log thread is basically a loop that runs every second or so and breaks only when the application is being closed. 日志线程基本上是一个每隔一秒左右运行循环,并且仅在应用程序关闭时才会中断。

Basically, every second or so, the log thread writes to a log file (text file) if there are messages to output. 基本上,每隔一秒左右,如果有要输出的消息,日志线程会写入日志文件(文本文件)。 My problem is, on the main thread I have a move function which can change the path to the log file (and other files) and it moves the old files to the new location. 我的问题是,在主线程上我有一个移动功能,它可以改变日志文件(和其他文件)的路径,并将旧文件移动到新位置。 My problem is, what happens if the log thread is writing to the log file and the move function tries to move the file or vice versa? 我的问题是,如果日志线程写入日志文件并且移动函数尝试移动文件,反之亦然,会发生什么? And how would I help prevent this from happening? 我将如何帮助防止这种情况发生?

The program runs fine if I don't change the save path but I am worried that the main thread and log thread may become deadlocked if they both try to do something to the log file at the same time. 如果我不更改保存路径,程序运行正常,但我担心如果主线程和日志线程都试图同时对日志文件执行某些操作,则主线程和日志线程可能会死锁。 I've done some research into this but all I can come up with are ways to stop multiple threads from reading/writing to a file at the same time, not moving the file and writing to it like I am. 我已经对此做了一些研究,但我能想到的是同时阻止多个线程读/写文件的方法,而不是移动文件并像我一样写入文件。 So really, is there some way that the log thread can tell the main thread it is using the log file and the main thread can use the log file when it is done or vice versa? 那么,是否有某种方式,日志线程可以告诉主线程它正在使用日志文件,主线程可以在完成时使用日志文件,反之亦然?

Multi threading is something I have always wanted to try but was always put off by the complexity of things like this. 多线程是我一直想尝试的东西,但总是被这样的事情的复杂性所拖延。 It certainly requires much more logic to stop things from going wrong. 它当然需要更多的逻辑来阻止事情出错。

Help would be greatly appreciated as this is one of the last things that the application needs in order to prevent massive errors (at least that I can tell, that's what testing is for really). 帮助将不胜感激,因为这是应用程序需要的最后一件事,以防止大量错误(至少我可以告诉,这是真正的测试)。

Edit: What I am really looking for is some way that the two threads can "communicate" to tell each other when they are accessing the log file so that they don't try to access the same file at the same time. 编辑:我真正想要的是两个线程可以“通信”以告知对方何时访问日志文件,以便他们不会同时尝试访问同一文件。

每当主线程需要复制文件时,我就会一起杀死你的日志线程,然后再启动一个新的日志线程。

Your question guides this in the wrong direction, you might get a better answer if you explain what your end goal is. 你的问题指导方向错误,如果你解释你的最终目标是什么,你可能会得到更好的答案。

Usually this is done differently. 通常情况不同。 Multiple threads log to an in memory buffer. 多个线程登录到内存缓冲区。 Each thread has its own buffer, so there is no buffer contention. 每个线程都有自己的缓冲区,因此没有缓冲区争用。 When the buffer of a thread is full, a new buffer is created and swapped with the full buffer. 当线程的缓冲区已满时,将创建一个新缓冲区并与完整缓冲区交换。 Logging continues to the new buffer, while the old buffer is sent to a thread whose purpose is just to write buffers to files and rotate the log files. 记录继续到新缓冲区,而旧缓冲区被发送到一个线程,其目的只是将缓冲区写入文件并旋转日志文件。 This allows multiple threads to log in memory very fast. 这允许多个线程非常快速地登录内存。 Since it's only one thread that actually interacts with the file, there's no conflict around the file movement operation. 由于它只是一个实际与文件交互的线程,因此文件移动操作没有冲突。

Regarding your question, assuming file moving does not happen often, you could use a slim multiple reader, single writer lock https://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim%28v=vs.110%29.aspx to achieve that (if you have multiple threads logging and only one moving the file), otherwise if you have just two threads, a plain lock(obj) statement will suffice. 关于你的问题,假设文件移动不经常发生,你可以使用纤薄的多个读取器,单个写入器锁定https://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim%28v=vs.110 %29.aspx实现这一点(如果你有多个线程记录而只有一个移动文件),否则如果你只有两个线程,普通锁(obj)语句就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM