title: Why would I want to lock two mutexes in one function - that too with deferred lock?
body: <p><a href="https://en.cppreference.com/w/cpp/thread/lock_tag" rel="nofollow noreferrer">https://en.cppreference.com/w/cpp/thread/lock_tag</a></p>
  
  <pre><code>void transfer(bank_account &amp;from, bank_account &amp;to, int amount)
  {
      // lock both mutexes without deadlock
      std::lock(from.m, to.m);
      // make sure both already-locked mutexes are unlocked at the end of scope
      std::lock_guard&lt;std::mutex&gt; lock1(from.m, std::adopt_lock);
      std::lock_guard&lt;std::mutex&gt; lock2(to.m, std::adopt_lock);
  
  // equivalent approach:
  //    std::unique_lock&lt;std::mutex&gt; lock1(from.m, std::defer_lock);
  //    std::unique_lock&lt;std::mutex&gt; lock2(to.m, std::defer_lock);
  //    std::lock(lock1, lock2);
  
      from.balance -= amount;
      to.balance += amount;
  }
  </code></pre>
  
  <p><em>What do they gain by locking two mutexes at once?<br>
  What have they gained by defered lock here?</em>  </p>
  
  <p>Please explain the reason behind that decision of theirs.</p>