Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a cause that you might not see coming: <a href="http://msdn.microsoft.com/en-us/magazine/cc872851.aspx" rel="nofollow noreferrer">false sharing</a> because those 4 ints all sit side by side in memory.</p> <p><strong>Update - MSDN mags from previous years are only available as <code>.chm</code> files now - so you have to grab the <a href="https://msdn.microsoft.com/magazine/msdn-magazine-issues" rel="nofollow noreferrer">'October 2008' edition of the MSDN Mag from here</a> and, after downloading, you must remember to right-click and 'unblock' the file from the file properties dialog in Windows Explorer (other OSs are available!) before opening it. You're looking for a column called '.Net Matters' by Stephen Toub, Igor Ostrovsky, and Huseyin Yildiz</strong></p> <p>The article (read it all - it's brilliant) shows how values that are side by side in memory can end up causing blocking when updated because they all sit on the same cache line. This is very low-level blocking that you can't disable from your .Net code. You can, however force the data to be spaced further apart so that you guarantee, or at least increase the likelihood, that each value will be on a different cache line.</p> <p>The article uses arrays - but it's just possible it's affecting you here.</p> <p>To follow up the suggestion below, you might be able to prove/disprove this by changing your code ever-so-slightly:</p> <pre><code>class Program { class CounterHolder { private int[] fakeInts = new int[1024]; public int Value = 0; } static CounterHolder counter1 = new CounterHolder(); static CounterHolder counter2 = new CounterHolder(); static CounterHolder counter3 = new CounterHolder(); static CounterHolder counter4 = new CounterHolder(); </code></pre> <p>And then modify your thread functions to manipulate the public field <code>Value</code> on each of the counter holders.</p> <p>I've made those arrays really much bigger than they need to be in the hope that it'll prove it better :)</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