title: Multiple FileStreams for reading/writing different sections of same file concurrently in independent threads
body: <p>I have a large disk file (around 8 GB) containing several million records that I need to read, process in memory, and write back to another file. All the records are of a fixed length (say, 100 bytes).</p>
  <p>I was thinking of parallelizing my process to run on multiple threads (typically 4–8), each of which would be (exclusively) assigned a particular section of the file to process (for example, 1 GB chunks). Since each thread would restrict its reads and writes to the section of the file it has been assigned, there is no risk of race hazards from my code.</p>
  <p>Am I allowed to initialize multiple threads, each with its own <code>FileStream</code>, for reading from / writing to the same file without locking, without risking corruption? Assume that the target file has been expanded to its full size in advance (using <code>FileStream.SetLength</code>), and that the appropriate <code>FileShare</code> flag is specified when opening each <code>FileStream</code>.</p>
  <p>Also, would I risk incurring slow-downs due to loss of buffering if multiple threads access the same file simultaneously? I am concerned about the “Detection of stream position changes” section in the MSDN documentation on the <a href="http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx" rel="nofollow noreferrer"><code>FileStream</code> class</a>, which states:</p>
  <blockquote>
  <p>When a <code>FileStream</code> object does not have an exclusive hold on its handle, another thread could access the file handle concurrently and change the position of the operating system's file pointer that is associated with the file handle. […]</p>
  <p>If an unexpected change in the handle position is detected in a call to the <code>Read</code> method, the .NET Framework discards the contents of the buffer and reads the stream from the file again. This can affect performance, depending on the size of the file and any other processes that could affect the position of the file stream.</p>
  </blockquote>
  <p>Would this apply in my case, or are the file handles created by <code>FileStream</code> instances distinct and independent, even if accessing the same file?</p>