Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Old question, new answer:</p> <p>Imho, a better way to deal with the dead-lock problem of the original copy assignment operator is:</p> <pre><code> A&amp; operator=(const A&amp; other) { if (this!=&amp;other) { std::unique_lock&lt;std::mutex&gt; _mylock(mtx, std::defer_lock), _otherlock(other.mtx, std::defer_lock); std::lock(_mylock, _otherlock); i = other.i; } return *this; } </code></pre> <p>I.e. use <code>std::lock(L1, L2)</code> to simultaneously lock the two mutexes without fear of deadlock. This is likely to be higher performance than the copy/swap idiom, especially if the member data consists of <code>std::vector</code>, <code>std::string</code>, or types that contain vectors and/or strings.</p> <p>In C++1y (we hope y is 4), there is a new <code>&lt;shared_mutex&gt;</code> header providing read/write lock capability which <em>may</em> provide a performance boost (performance testing would be necessary for specific use cases to confirm that). Here is how it would be used:</p> <pre><code>#include &lt;mutex&gt; #include &lt;shared_mutex&gt; class A { private: int i; mutable std::shared_mutex mtx; public: A() : i(), mtx() { } A(const A&amp; other) : i(), mtx() { std::shared_lock&lt;std::shared_mutex&gt; _lock(other.mtx); i = other.i; } A&amp; operator=(const A&amp; other) { if (this!=&amp;other) { std::unique_lock&lt;std::shared_mutex&gt; _mylock(mtx, std::defer_lock); std::shared_lock&lt;std::shared_mutex&gt; _otherlock(other.mtx, std::defer_lock); std::lock(_mylock, _otherlock); i = other.i; } return *this; } int get() const { std::shared_lock&lt;std::shared_mutex&gt; _mylock(mtx); return i; } }; </code></pre> <p>I.e. this is very similar to the original code (modified to use <code>std::lock</code> as I've done above). But the member mutex type is now <code>std::shared_mutex</code> instead of <code>std::mutex</code>. And when protecting a <code>const A</code> (and assuming no mutable members besides the mutex), one need only lock the mutex in "shared mode". This is easily done using <code>shared_lock&lt;shared_mutex&gt;</code>. When you need to lock the mutex in "exclusive mode", you can use <code>unique_lock&lt;shared_mutex&gt;</code>, or <code>lock_guard&lt;shared_mutex&gt;</code> as appropriate, and in the same manner as you would have used this facilities with <code>std::mutex</code>.</p> <p>In particular note that now many threads can simultaneously copy construct from the same <code>A</code>, or even copy assign <em>from</em> the same <code>A</code>. But only one thread can still copy assign <strong>to</strong> the same <code>A</code> at a time.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload