Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a classical example of data race.</p> <p>Now, let's take a closer look at what add() does:</p> <pre><code>add() { x = x + 1; } </code></pre> <p>This translates to:</p> <ol> <li>Give me the most recent value of X and store it in my private workspace</li> <li>Add 1 to that value that is stored in my private workspace</li> <li>Copy what I have in my workspace to the memory that I copied from (that is globally accessible).</li> </ol> <p>Now, before we go further in explaining this, you have something called <a href="http://en.wikipedia.org/wiki/Context_switch" rel="nofollow">context switch</a>, which is the process by which your operating system divides your processor's time among different threads and processes. This process usually gives your threads a finite amount of processor time (on windows it is about 40 milliseconds) and then interrupts that work, copies everything the processor have in its registers (and by thus preserve it's state) and switch to the next task. This is called <a href="http://en.wikipedia.org/wiki/Task_scheduling" rel="nofollow">Round-robin task scheduling</a>. </p> <p>You have no control over when you're processing is going to be interrupted and transferred to another thread. </p> <p>Now imagine you have two threads doing the same:</p> <pre><code>1. Give me the most recent value of X and store it in my private workspace 2. Add 1 to that value that is stored in my private workspace 3. Copy what I have in my workspace to the memory that I copied from (that is globally accessible). </code></pre> <p>and X is equal to 1 before any of them runs.</p> <p>The first thread might execute the first instruction and store in it's private workspace the value of X that was most recent at the time it was working on - 1. Then a context-switch occurs and the operating system interrupts your threads and gives control to the next task in queue, that happens to be the second thread. The second thread also reads the value of X which is equal to 1.</p> <p>Thread number two manages to run to completion - it adds 1 to the value it "downloaded" and "uploads" the calculated value.</p> <p>The operating system forces a context switch again.</p> <p>Now the first thread continues execution at the point where it was interrupted. It will still think that the most recent value is 1, it will increment that value by one and save the result of it's computation to that memory area. And this is how data races occur. You expect the final result to be 3 but it is 2.</p> <p>There are many ways to avoid this problem such as <a href="http://en.wikipedia.org/wiki/Lock_%28computer_science%29" rel="nofollow">locks/mutexes</a>, <a href="http://en.wikipedia.org/wiki/Compare_and_swap" rel="nofollow">compare and swap</a> or <a href="http://en.wikipedia.org/wiki/Atomic_operations" rel="nofollow">atomic operations</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