title: ReleaseSemaphore does not release the semaphore
body: <p>(In short: main()'s WaitForSingleObject hangs in the program below).</p>
  
  <p>I'm trying to write a piece of code that dispatches threads and waits for them to finish before it resumes. Instead of creating the threads every time, which is costly, I put them to sleep. The main thread creates X threads in CREATE_SUSPENDED state.</p>
  
  <p>The synch is done with a semaphore with X as MaximumCount. The semaphore's counter is put down to zero and the threads are dispatched. The threds perform some silly loop and call ReleaseSemaphore before they go to sleep. Then the main thread uses WaitForSingleObject X times to be sure every thread finished its job and is sleeping. Then it loops and does it all again.</p>
  
  <p>From time to time the program does not exit. When I beak the program I can see that WaitForSingleObject hangs. This means that a thread's ReleaseSemaphore did not work. Nothing is printf'ed so supposedly nothing went wrong.</p>
  
  <p>Maybe two threads shouldn't call ReleaseSemaphore at the exact same time, but that would nullify the purpose of semaphores...</p>
  
  <p>I just don't grok it...</p>
  
  <p>Other solutions to synch threads are gratefully accepted!</p>
  
  <pre><code>#define TRY  100
  #define LOOP 100
  
  HANDLE *ids;
  HANDLE semaphore;
  
  DWORD WINAPI Count(__in LPVOID lpParameter)
  { 
   float x = 1.0f;   
   while(1)
   { 
    for (int i=1 ; i&lt;LOOP ; i++)
     x = sqrt((float)i*x);
    while (ReleaseSemaphore(semaphore,1,NULL) == FALSE)
     printf(" ReleaseSemaphore error : %d ", GetLastError());
    SuspendThread(ids[(int) lpParameter]);
   }
   return (DWORD)(int)x;
  }
  
  int main()
  {
   SYSTEM_INFO sysinfo;
   GetSystemInfo( &amp;sysinfo );
   int numCPU = sysinfo.dwNumberOfProcessors;
  
   semaphore = CreateSemaphore(NULL, numCPU, numCPU, NULL);
   ids = new HANDLE[numCPU];
  
   for (int j=0 ; j&lt;numCPU ; j++)
    ids[j] = CreateThread(NULL, 0, Count, (LPVOID)j, CREATE_SUSPENDED, NULL);
  
   for (int j=0 ; j&lt;TRY ; j++)
   {
    for (int i=0 ; i&lt;numCPU ; i++)
    {
     if (WaitForSingleObject(semaphore,1) == WAIT_TIMEOUT)
      printf("Timed out !!!\n");
     ResumeThread(ids[i]);  
    }
    for (int i=0 ; i&lt;numCPU ; i++)
     WaitForSingleObject(semaphore,INFINITE);
    ReleaseSemaphore(semaphore,numCPU,NULL);
   }
   CloseHandle(semaphore);
   printf("Done\n");
   getc(stdin);
  }
  </code></pre>