title: Acquire a lock on two mutexes and avoid deadlock
body: <p>The following code contains a potential deadlock, but seems to be necessary: to safely copy data to one container from another, both containers must be locked to prevent changes from occurring in another thread.</p>
  
  <pre><code>void foo::copy(const foo &amp; rhs)
  {
      pMutex-&gt;lock();
      rhs.pMutex-&gt;lock();
      // do copy
  }
  </code></pre>
  
  <p>Foo has an STL container and "do copy" essentially consists of using std::copy. How do I lock both mutexes without introducing deadlock?</p>