1
votes

I have two threads: reader and writer. Both communicate through memory allocated by CreateFileMapping + MapViewOfFile. Both work relativelly fast. Both work async.

PROBLEM: reader gets zero values from time to time. And I am absolutelly sure that it is not what writer provided. Values provided by writer are never zero.

I believe that occures when writer and reader are trying to access memory simultaneoussly. I was hoping that "CreateFileMapping + MapViewOfFile" mechanism would automatically resolve that kind of problems. Am I mistaken? Do I need to add Events to synchronize access to my shared memory?

1
Writing to memory takes time. While you're writing to your shared memory, you are in an indefinite state. Once the write operation finishes, the changes are reflected to all mapped views. Regardless of how you go about this, you'll have to synchronize reading and writing, in order to establish consistent results. - IInspectable
1) Why do I get zero values? Not previous values? 2) Is it possible to detect that "read" function failed to get data? Some kind of GetLastError? - walruz
It is unclear why zero is a problem: you don't tell what you read, what you write, how you write. It could possibly be that write of zero is a part of write operation and reader takes that while write is in the middle of its task. - Roman R.
Zero is a problem because I am absolutelly sure that writer is putting different value. It is never zero. - walruz
You will need to post an MCVE. There isn't enough information in the question for us to guess at what you might be doing wrong, although in general you will need some kind of synchronization. - Harry Johnston

1 Answers

0
votes

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

  1. writing new records to memory
  2. updating the write pointer

Reading records means:

  1. read the write pointer to detect if there are new records
  2. 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.