title: CreateFileMapping and MapViewOfFile with interprocess (un)synchronized multithreaded access?
body: <p>I use a Shared Memory area to get som data to a second process.</p>
  
  <p>The first process uses <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366537%28v=vs.85%29.aspx" rel="nofollow noreferrer"><code>CreateFileMapping</code></a><code>(INVALID_HANDLE_VALUE, ..., PAGE_READWRITE, ...)</code> and <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366761%28v=vs.85%29.aspx" rel="nofollow noreferrer"><code>MapViewOfFile</code></a><code>( ... FILE_MAP_WRITE)</code>.</p>
  
  <p>The second process uses <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366791%28v=vs.85%29.aspx" rel="nofollow noreferrer"><code>OpenFileMapping</code></a><code>(FILE_MAP_WRITE, ...)</code> and <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366761%28v=vs.85%29.aspx" rel="nofollow noreferrer"><code>MapViewOfFile</code></a><code>( ... FILE_MAP_WRITE)</code>.</p>
  
  <p>The <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366761%28v=vs.85%29.aspx" rel="nofollow noreferrer">docs</a> state:</p>
  
  <blockquote>
    <p>Multiple views of a file mapping object 
    are <em>coherent</em> 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. (...)</p>
    
    <p>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.</p>
    
    <p>The exception is related to remote files. (...)</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>The current pattern I have (simplified) is like this:</p>
  
  <pre><code>Process1                    |  Process2
  ...                         |  ...
  /* write to shared mem, */  |  ::WaitForSingleObject(hDataReady); // real code has error handling
  /* then: */
  ::SetEvent(hDataReady);     |  /* read from shared mem after wait returns */
  ...                         |  ...
  </code></pre>
  
  <p>Is this enough synchronization, even for shared memory?</p>
  
  <p>What sync is needed in general between the two processes?</p>
  
  <p>Note that inside of one single process, the call to <code>SetEvent</code> would certainly constitute a <a href="https://stackoverflow.com/questions/286629/what-is-a-memory-fence">full memory barrier</a>, but it isn't completely clear to me whether that holds for shared memory across processes.</p>