Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to a file from multiple threads asynchronously c#

Here is my situation. I would like to make writing to the file system as efficient as possible in my application. The app is multi-threaded and each thread can possibly write to the same file. Is there a way that I can write to the file asynchronously from each thread without having the writes in the different threads bang heads together, so to speak?

I'm using C# and .NET 3.5, and I do have the Reactive Extensions installed as well.

like image 252
Jeffrey Cameron Avatar asked Aug 17 '10 23:08

Jeffrey Cameron


People also ask

Can multiple threads write to the same file C?

While fread() and fwrite() are thread safe, the stream buffer represented by the FILE* is not. So you can have multiple threads accessing the same file, but not via the same FILE* - each thread must have its own, and the file to which they refer must be shareable - which is OS dependent.

Can multiple threads write to the same file?

Multiple threads can read and write the same file in several situations: Multiple threads read the same file at the same time. In this case, there is no conflict. If multiple threads write the same file at the same time, write data will be lost.

What is thread in c# net?

A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.

Is Appendalltext thread safe?

It is thread safe in the sense that it opens the file with Read sharing, so assuming your filesystem honors file locks, only one thread will be allowed to write to the file at a time.


2 Answers

For those who prefer code, I am using following to do remote logging from web apps...

public static class LoggingExtensions {     static ReaderWriterLock locker = new ReaderWriterLock();     public static void WriteDebug(this string text)     {         try         {             locker.AcquireWriterLock(int.MaxValue); //You might wanna change timeout value              System.IO.File.AppendAllLines(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", ""), "debug.txt"), new[] { text });         }         finally         {             locker.ReleaseWriterLock();         }     } } 

Hope this saves you some time

like image 130
Matas Vaitkevicius Avatar answered Oct 04 '22 10:10

Matas Vaitkevicius


Have a look at Asynchronous I/O. This will free up the cpu to continue with other tasks.
Combine with ReaderWriterLock as @Jack B Nimble mentioned

If by

writing to the file system as efficient as possible

you mean making the actual file I/O as fast as possible you are going to have a hard time speeding it up much, disk is just physically slower. Maybe SSD's?

like image 45
µBio Avatar answered Oct 04 '22 12:10

µBio