title: Deadlock simulation using std::mutex
body: <p>I have following example:</p>
  
  <pre><code>template &lt;typename T&gt;
  class container
  {
  public:
      std::mutex _lock;
      std::set&lt;T&gt; _elements;
  
      void add(T element)
      {
          _elements.insert(element);
      }
  
      void remove(T element)
      {
          _elements.erase(element);
      }
  };
  
  void exchange(container&lt;int&gt;&amp; cont1, container&lt;int&gt;&amp; cont2, int value)
      {
          cont1._lock.lock();
          std::this_thread::sleep_for(std::chrono::seconds(1));
  
          cont2._lock.lock();
  
          cont1.remove(value);
          cont2.add(value);
  
          cont1._lock.unlock();
          cont2._lock.unlock();
      }
  
      int main() 
      {
          container&lt;int&gt; cont1, cont2;
  
          cont1.add(1);
          cont2.add(2);
  
          std::thread t1(exchange, std::ref(cont1), std::ref(cont2), 1);
          std::thread t2(exchange, std::ref(cont2), std::ref(cont1), 2);
  
          t1.join();
          t2.join();
  
          return 0;
      }
  </code></pre>
  
  <p>In this case I'm expiriencing a deadlock. But when I use std::lock_guard instead of manually locking and unlocking mutextes I have no deadlock. Why?</p>
  
  <pre><code>void exchange(container&lt;int&gt;&amp; cont1, container&lt;int&gt;&amp; cont2, int value)
  {
      std::lock_guard&lt;std::mutex&gt;(cont1._lock);
      std::this_thread::sleep_for(std::chrono::seconds(1));
  
      std::lock_guard&lt;std::mutex&gt;(cont2._lock);
  
      cont1.remove(value);
      cont2.add(value);
  }
  </code></pre>