title: C++ :deque iterator not dereferencable
body: <p>I'm trying to do a simple program to learn about Threads and concurrency in C++. I've created a template class named MyQueue which implements a queue and some variables to handle the synchronization of threads.</p>
  
  <p>The class has just two functions to get and put items from the queue and one function to close the queue and avoid further access. </p>
  
  <p>The program compiles just fine but when debugging it gets the following error:</p>
  
  <p><strong>Expression: deque iterator not dereferencable</strong></p>
  
  <p>This can happen no matter what the thread is doing, if get or put items.</p>
  
  <p>Here is the code: </p>
  
  <pre><code>template &lt;class T&gt; class MyQueue{
  queue&lt;T&gt; myqueue;
  int N;
  bool open;
  mutex m,m_open;
  condition_variable cv,cv2;
  
  public:
  MyQueue(int size){
      N=size;
      open=true;
  }
  
  bool isOpen(){ 
      lock_guard&lt;mutex&gt; lg(m_open);
      return open;
  }
  
  void close(){
      lock_guard&lt;mutex&gt; lg(m_open);
      open=false;
      cv.notify_all();
      cv2.notify_all();
  }
  
  bool get(T &amp;t){
      if(isOpen()==false)return false;
      if(myqueue.size()&gt;0){
          {
              lock_guard&lt;mutex&gt; lg(m);
              t=myqueue.front();
              myqueue.pop();
              cv.notify_one(); 
          }
      }else{
          unique_lock&lt;mutex&gt; ul(m);
          cv2.wait(ul); 
          if(!isOpen()) return false;
          t=myqueue.front();
          myqueue.pop();
          cv.notify_one();
      }
      return true;
  }
  
  
  
  bool put(T t){
      if(!isOpen())return false;
      if(myqueue.size()&lt;N){
          {
          lock_guard&lt;mutex&gt; lg(m); 
          myqueue.push(t);
          cv2.notify_one(); 
          }
      }else{
          unique_lock&lt;mutex&gt; ul(m);
          cv.wait(ul);
          if(!isOpen())return false;
          myqueue.push(t);
          cv2.notify_one();
      }
      return true;
  }
  
  };
  </code></pre>