Consider the instructions that might be generated for a statement like i++
. Of course this will depend upon your architecture/instruction set, but it will probably be something along the lines of:
LOAD @i, r0 ;load the value of 'i' into a register from memory
ADD r0, 1 ;increment the value in the register
STORE r0, @i ;write the updated value back to memory
Now consider how multithreading would be implemented in an operating-system, regardless of how many cores the machine has. At the most basic level, the OS is going to need some facility to interrupt the execution of the current thread, save its state, and perform a context switch to a different thread. The OS has no automatic way of knowing which instructions inside of a user thread should be treated as an atomic operation, and has the ability to initiate a context switch between any two instructions.
So what happens if the OS performs a context switch from one thread to the other between LOAD
and ADD
? Let's say that i
started out with a value of 0, so r0
will be set to 0 when the first thread gets swapped out. The OS will save this value as part of that thread's state. Now the second thread runs, and performs the same LOAD
statement. The value in memory is still 0, so r0
gets 0 loaded into it again. The thread increments the value and writes it back to memory, setting the value of i
to 1. Now the first thread resumes execution, and the operating-system restores the value of r0
to 0 as part of its context switch. The first thread now performs its increment, setting r0
to 1, and the value of 1 gets stored in i
again. Now the value of i
is incorrect because two increments have been applied, but the value has only increased by 1.
So in a nutshell, even though i++
is a single statement in a high-level language, it generates multiple assembly-language instructions, and those instructions will not be treated as atomic by the operating-system/runtime environment unless you add extra synchronization logic around them.