title: Simple multithreading mutex example is incorrect
body: <p>I expect to get numbers from 0 to 4 in random order, but instead, I have some unsynchronized mess</p>
  
  <p>What i do wrong?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;windows.h&gt;
  #include &lt;process.h&gt;
  
  using namespace std;
  
  void addQuery(void *v );
  
  HANDLE ghMutex;
  
  int main()
  {
      HANDLE hs[5];
      ghMutex = CreateMutex( NULL, FALSE, NULL);         
      for(int i=0; i&lt;5; ++i)
      {
          hs[i] = (HANDLE)_beginthread(addQuery, 0, (void *)&amp;i);
          if (hs[i] == NULL) 
          {
              printf("error\n"); return -1;
          }
      }
  
      printf("WaitForMultipleObjects return: %d error: %d\n",
           (DWORD)WaitForMultipleObjects(5, hs, TRUE, INFINITE), GetLastError());
  
  
      return 0;
  }
  
  void addQuery(void *v )
  {
      int t = *((int*)v);
      WaitForSingleObject(ghMutex, INFINITE);
  
      cout &lt;&lt; t &lt;&lt; endl;
  
      ReleaseMutex(ghMutex);
      _endthread();
  }
  </code></pre>