title: Semaphore and Critical Section issue on multiple threads
body: <p>I am having an issue with my multithreaded code and hope someone can help me out.</p>
  
  <p>I wish to print on the console all files and folder starting from a folder given as an argument. I use this function for the enumeration:</p>
  
  <pre><code>void enumerate(char* path) {
      HANDLE hFind;
      WIN32_FIND_DATA data;
  
      char *fullpath = new char[strlen(path) - 1];
      strcpy(fullpath, path);
      fullpath[strlen(fullpath) - 1] = '\0';
  
      hFind = FindFirstFile(path, &amp;data);
      do {
          if (hFind != INVALID_HANDLE_VALUE) {
              if (strcmp(data.cFileName, ".") != 0 &amp;&amp; strcmp(data.cFileName, ".."))
              {
                  EnterCriticalSection(&amp;crit);
                  queue.push(data.cFileName);
                  LeaveCriticalSection(&amp;crit);
                  ReleaseSemaphore(semaphore, 1, NULL);
                  if (data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
                  {
                      strcat(fullpath, data.cFileName);
                      strcat(fullpath, "\\*");
                      enumerate(fullpath);
                  }
              }
          }
      } while (FindNextFile(hFind, &amp;data));
      FindClose(hFind);
  
      return;
  }
  </code></pre>
  
  <p>When I find a file or a folder, I want to add it to a global queue and have my worker threads print it to the console. My worker threads function is:</p>
  
  <pre><code>DWORD WINAPI print_queue(LPVOID param) {
      while (1) {
          WaitForSingleObject(semaphore, INFINITE);
          EnterCriticalSection(&amp;crit);
          char *rez = queue.front();
          queue.pop();
          LeaveCriticalSection(&amp;crit);
  
          if (strcmp(rez, "DONE") == 0)
              break;
          else
              std::cout &lt;&lt; rez &lt;&lt; std::endl;
      }
  
      return 1;
  }
  </code></pre>
  
  <p>In <code>main</code>, I initialize the semaphore and critical section, both variables declared globally:</p>
  
  <pre><code>semaphore = CreateSemaphore(NULL, 0,1, NULL);
  InitializeCriticalSection(&amp;crit);
  </code></pre>
  
  <p>Then create 4 threads:</p>
  
  <pre><code>thread1 = CreateThread(NULL, 0, print_queue, NULL, 0, &amp;tId1);
  thread2 = CreateThread(NULL, 0, print_queue, NULL, 0, &amp;tId2);
  thread3 = CreateThread(NULL, 0, print_queue, NULL, 0, &amp;tId3);
  thread4 = CreateThread(NULL, 0, print_queue, NULL, 0, &amp;tId4);
  </code></pre>
  
  <p>I then call the <code>enumerate()</code> function and for strings to the queue that will signal my threads to stop when those strings are reached:</p>
  
  <pre><code>for (int p = 0; p&lt;4; p++)
  {
      EnterCriticalSection(&amp;crit);
      queue.push(done);
      LeaveCriticalSection(&amp;crit);
      ReleaseSemaphore(semaphore, 1, NULL);
  }
  </code></pre>
  
  <p>Those 4 strings are the stop condition for my threads. I then wait for the threads:</p>
  
  <pre><code>HANDLE * threadArray = new HANDLE[4];
  
  threadArray[0] = thread1;
  threadArray[1] = thread2;
  threadArray[2] = thread3;
  threadArray[3] = thread4;
  
  WaitForMultipleObjects(4, threadArray, TRUE, INFINITE);
  </code></pre>
  
  <p>And close the semaphore and critical section:  </p>
  
  <pre><code>CloseHandle(semaphore);
  DeleteCriticalSection(&amp;crit);
  </code></pre>
  
  <p>For some reason, the output is random garbage and I can't figure out why.</p>
  
  <p>This is an example output:</p>
  
  <pre>
  te(L┤(L
  ┤(L 
   ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
  ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
  ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠°┐*╧wM3╧weµFC4
  ╠╠╠╠╠
  </pre>
  
  <p>My logic was to start the semaphore on 0, enter the critical section whenever operations happened on a queue to protect my data, increment the semaphore in the <code>enumerate()</code> function and decrease it in <code>print_queue()</code>.</p>
  
  <p>What might be the problem?</p>