Question: I've been reading [this article](http://preshing.com/20130618/atomic-vs-non-atomic-operations/) about atomic operations, and it mentions 32-bit integer assignment being atomic on x86, as long as the variable is naturally aligned.

Why does natural alignment assure atomicity?

Answer: 
Naturally aligned means that the address of the type is a multiple of the size of the type.

For example, a byte can be at any address, a short (assuming 16 bits) must be on a multiple of 2, an int (assuming 32 bits) must be on a multiple of 4, and a long (assuming 64 bits) must be on a multiple of 8.

In the event that you access a piece of data that is not naturally aligned the CPU will either raise a fault or will read/write the memory, but not as an atomic operation. The action the CPU takes will depend on the architecture.

For example, image we've got the memory layout below:

```
01234567
...XXXX.

```

and

```
int *data = (int*)3;

```

When we try to read `*data` the bytes that make up the value are spread across 2 int size blocks, 1 byte is in block 0-3 and 3 bytes are in block 4-7. Now, just because the blocks are logically next to each other it doesn't mean they are physically. For example, block 0-3 could be at the end of a cpu cache line, whilst block 3-7 is sitting in a page file. When the cpu goes to access block 3-7 in order to get the 3 bytes it needs it may see that the block isn't in memory and signals that it needs the memory paged in. This will probably block the calling process whilst the OS pages the memory back in. 

After the memory has been paged in, but before your process is woken back up another one may come along and write a `Y` to address 4. Then your process is rescheduled and the CPU completes the read, but now it has read XYXX, rather than the XXXX you expected.
To answer your first question, a variable is naturally aligned if it exists at a memory address that is a multiple of its size.

If we consider only - as the article you linked does - *assignment instructions*, then alignment guarantees atomicity because MOV (the assignment instruction) is atomic by design on aligned data.

Other kinds of instructions, INC for example, need to be **LOCK**ed (an x86 prefix which gives exclusive access to the shared memory to the current processor for the duration of the prefixed operation) even if the data are aligned because they actually execute via multiple steps (=instructions, namely load, inc, store).