title: C++ "deque iterator not dereferencable" multithreading. WaitForSingleObject() lets the thread through
body: <p>When im calling WaitForSingleObject() in my Pop() function even if the event notempty is NOT set it doesnt stop it, i dont know what to do with it.
  it should wait for a number to be pushed, and even if it is not pushed, it should still wait for an INFINITE amount of time.</p>
  
  <p>same when calling WaitForSingleObject() in Push() when the event notfull is NOT set.</p>
  
  <p><img src="https://i.stack.imgur.com/xrNxg.png" alt="there you can see it"></p>
  
  <p>working in Microsoft VS RC 2017 with character set not set.</p>
  
  <p>I dont know how to get this working (waiting for infinite time to push/pop something).</p>
  
  <pre><code>   #include &lt;windows.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  #include &lt;time.h&gt;
  #include &lt;stack&gt;
  using namespace std;
  class MonitorStack {
  public:
      CRITICAL_SECTION cs_pop;
      CRITICAL_SECTION cs_push;
      stack&lt;int&gt; item;
      int MAXSIZE;
      HANDLE notempty, notfull;
      MonitorStack(int _size) {
          InitializeCriticalSection(&amp;cs_push);
          InitializeCriticalSection(&amp;cs_pop);
          MAXSIZE = _size;
          notempty = CreateEvent(NULL,TRUE,FALSE, "notempty");
          notfull= CreateEvent(NULL, TRUE, TRUE, "notfull");
          SetEvent(notfull);
      }
      MonitorStack() {
          InitializeCriticalSection(&amp;cs_push);
          InitializeCriticalSection(&amp;cs_pop);
          int _size = 0;
          MAXSIZE = _size;
          notempty = CreateEvent(NULL, FALSE, FALSE, "notempty");
          notfull = CreateEvent(NULL, FALSE, TRUE, "notfull");
          SetEvent(notfull);
      }
      ~MonitorStack() {
          MAXSIZE = -1;
      }
      void  Push(int element){
                  if (item.size() &gt;= MAXSIZE) {
                      ResetEvent(notfull);
                  }
                  EnterCriticalSection(&amp;cs_push);
                  WaitForSingleObject(notfull, INFINITY);
                      item.push(element);
                      cout &lt;&lt; "pushed " &lt;&lt; element &lt;&lt; endl;
                      SetEvent(notempty);
                      if (item.size() == MAXSIZE) {
                          ResetEvent(notfull);
                      }
                  LeaveCriticalSection(&amp;cs_pop);
      }
      int  Pop(){
                  if (item.size() == 0) {
                      ResetEvent(notempty);
                  }
                  EnterCriticalSection(&amp;cs_pop);
                  WaitForSingleObject(notempty, INFINITY);
                  int num = item.top();
                  cout &lt;&lt; "popped " &lt;&lt; num &lt;&lt; endl;
                  item.pop();
                  SetEvent(notfull);
                  if (item.size() == 0) {
                      ResetEvent(notempty);
                  }
                  LeaveCriticalSection(&amp;cs_push);
                  return num;
      }
  };
  MonitorStack item;
  DWORD WINAPI consumerFunction(LPVOID data)
  {
      int* info = (int*)data;
      for (int i = 0; i &lt; *info; i++) {
          item.Pop();
      }
      return 0;
  }
  DWORD WINAPI producerFunction(LPVOID data)
  {
      int* info = (int*)data;
      int num;
      srand(time(NULL));
      for (int i = 0; i &lt; *info; i++) {
          num = (rand() % 10 + 1);
          item.Push(num);
      }
      return 0;
  }
  void main() {
      DWORD  IDThread;
      int cap;
      cout &lt;&lt; "enter the capacity of the stack" &lt;&lt; endl;
      cin &gt;&gt; cap;
      int producer, consumer;
      cout &lt;&lt; "enter the producer and consumer count!" &lt;&lt; endl;
      cin &gt;&gt; producer &gt;&gt; consumer;
      HANDLE  *consumerThread = new HANDLE [consumer];
      HANDLE *producerThread = new HANDLE[producer];
      cout &lt;&lt; "enter now for each producer the amount of items to produce" &lt;&lt; endl;
      int *producercount = new int[producer];
      int *consumercount = new int[consumer];
      item = MonitorStack(cap);
      for (int i = 0; i &lt; producer; i++)
      {
          cin &gt;&gt; producercount[i];
      }
      cout &lt;&lt; "enter now for each consumer the amount of items to consume" &lt;&lt; endl;
      for (int i = 0; i &lt; consumer; i++)
      {
          cin &gt;&gt; consumercount[i];
      }
      for (int i = 0; i &lt; producer; i++)
      {
          producerThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)producerFunction, (void*)&amp;(producercount[i]), 0, &amp;IDThread);
      }
      for (int i = 0; i &lt; consumer; i++)
      {
          consumerThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)consumerFunction, (void*)&amp;(consumercount[i]), 0, &amp;IDThread);
      }
      WaitForMultipleObjects(producer, producerThread, TRUE, INFINITE);
      WaitForMultipleObjects(consumer, consumerThread, TRUE, INFINITE);
      for (int i = 0; i &lt; producer; i++)
      {
          CloseHandle(producerThread[i]);
      }
      for (int i = 0; i &lt; consumer; i++)
      {
          CloseHandle(consumerThread[i]);
      }
      system("pause");
  }
  </code></pre>