Search code examples
c#ipcmemory-mapped-filesinter-process-communicat

Reading and writing in parallel to MemoryMappedFile


What will happen if I read and write in parallel in the same MemoryMappedFile? Is it necessary that I lock them via Mutext before reading as shown in the samples on MSDN?

Memory-Mapped Files


Solution

  • Each "View" created from your memory mapped file can only be accessed by a single thread at a time, however you can create multiple view streams and each thread can write or read to it at the same time.

    However, if multiple views try to write to the same location at the same time you may get data "intermixed" with each other. The mutex in the example is to prevent that mixing. If one app is only writing to the file and the other is only reading then there is no need for a mutex, you only need it for multiple writers.