views:

133

answers:

2

Hey all,

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.

+2  A: 

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?

BioBuckyBall
This doesn't seem to address the question, the "bang heads together" issue.
Hans Passant
Yes, how does one prevent the asynchronous writes from colliding?
Jeffrey Cameron
The same way you handle any resource contention in a multi-threaded system. Locks. ReadWriterLock (or ReaderWriterLockSlim with 4.0 / Parallel Extensions + 3.5) allows multiple reads to occur concurrently, so if that is something you want, use it.
BioBuckyBall
A: 

Use Reader / Writer locks to access the file stream.

Jack B Nimble