title: std::lock_guard won't unlock
body: <p>I'm trying to lock my list of mutexes in the following code so that only one thread can search it, unlock, lock or modify it at a time.</p>
  
  <pre><code>#include &lt;mutex&gt;
  #include &lt;map&gt;
  #include &lt;memory&gt;
  #include &lt;vector&gt;
  #include &lt;thread&gt;
  #include &lt;atomic&gt;
  #include &lt;iostream&gt;
  #include &lt;Windows.h&gt;
  
  struct MoveableMutex
  {
      std::mutex m;
      MoveableMutex() {}
      MoveableMutex(MoveableMutex const&amp;) {}
      MoveableMutex&amp; operator = (MoveableMutex const&amp;) { return *this; }
  };
  
  class Locks
  {
      private:
          static std::mutex map_lock;
          static std::uint32_t lock_count;
          std::map&lt;std::uint32_t, MoveableMutex&gt; locklist;
  
      public:
          std::uint32_t AddLock();
          void RemoveLock(std::uint32_t ID);
          void Lock(std::uint32_t ID);
          bool TryLock(std::uint32_t ID);
          void Unlock(std::uint32_t ID);
  };
  
  std::uint32_t Locks::lock_count = 0;
  std::mutex Locks::map_lock;
  
  std::uint32_t Locks::AddLock()
  {
      std::lock_guard&lt;std::mutex&gt; guard(map_lock);
      locklist.insert(std::make_pair(++lock_count, MoveableMutex()));
      return lock_count;
  }
  
  void Locks::RemoveLock(std::uint32_t ID)
  {
      std::lock_guard&lt;std::mutex&gt; guard(map_lock);
      auto it = locklist.find(ID);
      if (it != locklist.end())
      {
          it-&gt;second.m.unlock();
          locklist.erase(it);
      }
  }
  
  void Locks::Lock(std::uint32_t ID)
  {
      std::lock_guard&lt;std::mutex&gt; guard(map_lock);
      auto it = this-&gt;locklist.find(ID);
      if (it != this-&gt;locklist.end())
      {
          it-&gt;second.m.lock();
      }
  }
  
  bool Locks::TryLock(std::uint32_t ID)
  {
      std::lock_guard&lt;std::mutex&gt; guard(map_lock);
      auto it = this-&gt;locklist.find(ID);
      if (it != this-&gt;locklist.end())
      {
          return it-&gt;second.m.try_lock();
      }
      return false;
  }
  
  void Locks::Unlock(std::uint32_t ID)
  {
      std::lock_guard&lt;std::mutex&gt; guard(map_lock);
      auto it = this-&gt;locklist.find(ID);
      if (it != locklist.end())
      {
          it-&gt;second.m.unlock();
      }
  }
  
  int main()
  {
      Locks locklist;
      int i = locklist.AddLock();
      std::atomic&lt;bool&gt; stop(false);
      std::atomic&lt;bool&gt; stop2(false);
  
      std::thread o([&amp;]
      {
          locklist.Lock(i);
          while(!stop)
          {
              std::cout &lt;&lt; "Hey\n";
              Sleep(100);
          }
          locklist.Unlock(i);
      });
  
      std::thread t([&amp;]
      {
          locklist.Lock(i);
          while(!stop2)
          {
              std::cout &lt;&lt; "Hey2\n";
              Sleep(100);
          }
          locklist.Unlock(i);
      });
  
      Sleep(1000);
      stop = true;
      system("CLS");
      o.join();
  
      Sleep(1000);
      stop2 = true;
      t.join();
      return 0;
  }
  </code></pre>
  
  <p>However, with the <code>std::lock_guard</code> inside the <code>Unlock</code> function, it causes a deadlock. If I remove the lock_guard from the Unlock function, it works fine.</p>
  
  <p>Is there a reason the lock_guard isn't destructing or unlocking?</p>