I’ve got this little problem on thread.
int x = 0;
add() {
x=x+1;
}
If we run this in multiple threads, say 4 threads, is the final value of x=4 at every time or it could be 1,2,3 or 4.
Thanks
PS
lets say the atomic operations for the adding is like this,
LOAD A x
ADD A 1
LOAD x A
Then the final result will be 4. Am I right or what have I get wrong ?
This is a classical example of data race.
Now, let’s take a closer look at what add() does:
This translates to:
Now, before we go further in explaining this, you have something called context switch, 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 Round-robin task scheduling.
You have no control over when you’re processing is going to be interrupted and transferred to another thread.
Now imagine you have two threads doing the same:
and X is equal to 1 before any of them runs.
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.
Thread number two manages to run to completion – it adds 1 to the value it “downloaded” and “uploads” the calculated value.
The operating system forces a context switch again.
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.
There are many ways to avoid this problem such as locks/mutexes, compare and swap or atomic operations.