Views of a file is synchronized between applications if the backing file is local.
MSDN:
With one important exception, file views derived from any file mapping object that is backed by the same file are coherent or identical at a specific time. Coherency is guaranteed for views within a process and for views that are mapped by different processes.
The exception is related to remote files. Although CreateFileMapping works with remote files, it does not keep them coherent. For example, if two computers both map a file as writable, and both change the same page, each computer only sees its own writes to the page. When the data gets updated on the disk, it is not merged.
This of course only pertains completed writes and reads.
- backing file is local (including: backed by swap file)
- you do not try to read before the write has returned successfully
So if - as you say - writer and reader run async, you probably need your own synchronization mechanism.
This may be using events / mutices, or can just be data in the file.
Example:
the content of the file is a ring buffer of records. at the beginning (or end) of the mapped memory region, there is a write pointer.
Adding records means
- writing new records to memory
- updating the write pointer
Reading records means:
- read the write pointer to detect if there are new records
- if there are new records, read them
Order is important. For ring buffer, you usually also need to introduce a read pointer to prevent overwriting unread records.
Make sure you test the return values of the write / read operations and handle errors accordingly.
Remember that Memory Mapped File access may raise Access Violation exceptions that your application is expected to handle.