Using of Interlocked.Exchange for updating of references and Int32

Viewed 16696

It is known that a reference takes 4 Bytes of memory in 32 bit processor and 8 Bytes - in 64 bit processor. So, processors guarantee that single reads from and writes to memory in increments of the natural word size of the machine will be carried out atomically. On the other hand there are 2 methods in Interlocked class:

public static int Exchange(
    ref int location1,
    int value
)

and

public static T Exchange<T>(
    ref T location1,
    T value
)
where T : class

So, the question is why Interlocked.Exchange is needed for Int32 and for reference types? Couldn't it be done safely by just using of simple assignment because it is atomic?

4 Answers
Related