I use a Shared Memory area to get som data to a second process.
The first process uses CreateFileMapping
(INVALID_HANDLE_VALUE, ..., PAGE_READWRITE, ...)
and MapViewOfFile
( ... FILE_MAP_WRITE)
.
The second process uses OpenFileMapping
(FILE_MAP_WRITE, ...)
and MapViewOfFile
( ... FILE_MAP_WRITE)
.
The docs state:
Multiple views of a file mapping object are coherent if they contain identical data at a specified time. This occurs if the file views are derived from any file mapping object that is backed by the same file. (...)
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. (...)
Since I'm just using the Shared Memory as is (backed by the paging file) I would have assumed that some synchronization is needed between processes to see a coherent view of the memory another process has written. I'm unsure however what synchronization would be needed exactly.
The current pattern I have (simplified) is like this:
Process1 | Process2
... | ...
/* write to shared mem, */ | ::WaitForSingleObject(hDataReady); // real code has error handling
/* then: */
::SetEvent(hDataReady); | /* read from shared mem after wait returns */
... | ...
Is this enough synchronization, even for shared memory?
What sync is needed in general between the two processes?
Note that inside of one single process, the call to SetEvent
would certainly constitute a full memory barrier, but it isn't completely clear to me whether that holds for shared memory across processes.