Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Suppose <strong>one</strong> of the threads needs to <strong>update the reference counter</strong> several times (e.g. when copying the <code>shared_ptr</code> around), while <strong>the other ones just need to access the pointed object</strong>: isn't this going to slow down the execution of <strong>all</strong> threads by making them compete for the same cache line?</p> </blockquote> <p>Yes, but is that a realistic scenario?</p> <p>In my code the threads that copy the <code>shared_ptr</code> do so because they want to share ownership of the object so they can use it. If the threads making all those reference-count updates don't care about the object, why are they bothering to share in ownership of it?</p> <p>You can mitigate the problem by passing around <code>const shared_ptr&amp;</code> references and only making (or destroying) a copy when you actually want to own and access the object, e.g. when transferring it across thread or module boundaries or when taking ownership of the object to use it.</p> <p>In general, intrusive reference counts outperform external reference counts (see <a href="http://www.boost.org/libs/smart_ptr/smarttests.htm" rel="nofollow">Smart Pointer Timings</a>) precisely <em>because</em> they're on a single cache line and so you don't need to use up two precious cache lines for the object and its refcount. Remember that if you've used up an extra cache line that's one less cache line for everything else, and something will get evicted and you'll get a cache miss when that is next needed.</p>
 

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