title: C++ Wait all threads to finish
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;process.h&gt;
  #include &lt;wtypes.h&gt;
  typedef unsigned int (__stdcall * THREAD_FUN_TYPE)(void *);
  
  int ThreadIp(void* param)
  {
      while(true)
      {
          printf("I'm runing!\n");
      }
      return 0;
  }
  
  
  int main()
  {
      int iThreadNum=100;
      HANDLE* phThreads = new HANDLE[iThreadNum];
      for (int i=0;i&lt;iThreadNum;++i)
      {
          phThreads[i]=(HANDLE*)_beginthreadex(NULL, 0,  (THREAD_FUN_TYPE)ThreadIp,NULL, NULL, NULL);
      }
  
      int nIndex = ::WaitForMultipleObjects(iThreadNum,phThreads,1,INFINITE);
      printf("End!\n");
      return 0;
  }
  </code></pre>
  
  <p>I want the program will halt at WaitForMultipleObjects until all thread are <strong>end</strong>(Not until all thread are created successfully).But the program will not halt at WaitForMultipleObjects,while all threads are still running. So I try to use SetEvent,but still the same problem:</p>
  
  <pre><code>int iThreadNum=100;
  HANDLE* phThreads = new HANDLE[iThreadNum];
  for (int i=0;i&lt;iThreadNum;++i)
  {
      phThreads[i]=CreateEvent(NULL, FALSE, FALSE,NULL);
      ResetEvent(phThreads[i]);
  }
  int nIndex = ::WaitForMultipleObjects(iThreadNum,phThreads,1,INFINITE);
  </code></pre>