Search code examples
c#multithreadingfile-ioasynchronousio

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.


Solution

  • 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?