title: DLL Mutex - an example
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4579168/dll-thread-safety">DLL thread safety</a>  </p>
  </blockquote>
  
  
  
  <p>Hi</p>
  
  <p>Im writting a DLL file in MS VS C++ express, which is loaded in multiple client applications at the same time, it uses shared memory with the other instances of the loaded DLL. Let us assume the DLL looks something like this:</p>
  
  <pre><code>#include stdafx.h  
  #pragma data_seg (".TEST")  
  //Shared variables  
  #pragma data_seg ()  
  #pragma comment(linker, "/section:.TEST,RWS")  
  _DLLAPI void __stdcall doCalc()  
  {  
  //Do critical stuff  
  }
  </code></pre>
  
  <p>If <code>doCalc</code> is called simultaneously from two or more clients the system will crash.
  How would I create a Mutex that "stalls" other calls if the function has already been called?
  Please give an example, as I have spent the last two hours trying to find a decent one on the internet ;)</p>
  
  <p>Thanks in advance.</p>