Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Is access through 'Instance' to the 'Toggle()' class threadsafe? If yes, by what assumptions, rules, etc. If no, why and how can I fix it?</p> </blockquote> <p>No, it's not threadsafe.</p> <p>Basically, both threads can run the <code>Toggle</code> function at the same time, so this could happen</p> <pre><code> // thread 1 is running this code if(value == 0) { value = 1; // RIGHT NOW, thread 2 steps in. // It sees value as 1, so runs the other branch, and changes it to 0 // This causes your method to return 0 even though you actually want 1 } else if(value == 1) { value = 0; } return value; </code></pre> <p>You need to operate with the following assumption.</p> <p>If 2 threads are running, they can and will interleave and interact with eachother randomly at any point. You can be half way through writing or reading a 64 bit integer or float (on a 32 bit CPU) and another thread can jump in and change it out from underneath you.</p> <p>If the 2 threads never access anything in common, it doesn't matter, but as soon as they do, you need to prevent them from stepping on each others toes. The way to do this in .NET is with locks.</p> <p>You can decide what and where to lock by thinking about things like this:</p> <p>For a given block of code, if the value of <code>something</code> got changed out from underneath me, would it matter? If it would, you need to lock that <code>something</code> for the duration of the code where it would matter.</p> <p>Looking at your example again</p> <pre><code> // we read value here if(value == 0) { value = 1; } else if(value == 1) { value = 0; } // and we return it here return value; </code></pre> <p>In order for this to return what we expect it to, we assume that <code>value</code> won't get changed between the read and the <code>return</code>. In order for this assumption to actually be correct, you need to lock <code>value</code> for the duration of that code block.</p> <p>So you'd do this:</p> <pre><code>lock( value ) { if(value == 0) ... // all your code here return value; } </code></pre> <p><strong>HOWEVER</strong></p> <p>In .NET you can only lock Reference Types. Int32 is a Value Type, so we can't lock it.<br> We solve this by introducing a 'dummy' object, and locking <em>that</em> wherever we'd want to lock 'value'.</p> <p>This is what <a href="https://stackoverflow.com/questions/42505/thread-safe-use-of-a-singletons-members/42517#42517">Ben Scheirman</a> is referring to.</p>
    singulars
    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.
    3. VO
      singulars
      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