Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That is because for most environments, incrementing a memory location is not an atomic operation.</p> <p>The sequence of events would be something like</p> <ul> <li>The compiler places the value 0 at the memory address 0xABCD</li> <li>Thread 1 reads 0xABCD into a register and increments the register value</li> <li>Thread 1 is interrupted by Thread 2</li> <li>Thread 2 reads the value 0 from the memory address 0xABCD into a register</li> <li>Thread 2 increments the value in the register</li> <li>Thread 2 writes the value from the register to 0xABCD</li> <li>Thread 2 reads the value 1 from 0xABCD, increments the register, and writes back again</li> <li>Thread 1 resumes</li> <li>Thread 1 writes the value from its register, 1, to 0xABCD, overwriting the value 2 that Thread 2 had previously written there.</li> </ul> <p>To ensure a consistent result, you must make the increment operation atomic. This is often done by placing a thread lock around the increment operation. For example, in C# that might look like</p> <pre><code>object mylock = new object(); ... lock (mylock) { myInt++; } </code></pre> <p>Alternatively, in the .NET environment, one could use Interlocked.Increment</p> <p><a href="http://msdn.microsoft.com/en-us/library/dd78zt0c.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/dd78zt0c.aspx</a></p> <p>Other environments have similar constructs.</p> <p>The best reference I have ever come across for threading in the .NET environment (but is a useful read no matter what your environment) is the following</p> <p><a href="http://www.albahari.com/threading/" rel="nofollow">http://www.albahari.com/threading/</a></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