Question

I have read somewhere that hyperthreading can make 32-bit int (on a 32-bit processor) read and write non-atomic even when it is boundary aligned. Can anyone explain how hyperthreading effects this?

Was it helpful?

Solution

If that were true, it would be some kind of horrible CPU bug that would be specific to one particular model or stepping. That 32-bit reads and writes are atomic is fundamental to the properties of the x86 platform and is relied on by Windows, Linux, and lots and lots of application software.

The only thing I can think of that this might have been referring to is transitioning from single-core CPUs without hyper-threading to single-physical-core CPUs with hyper-threading. On a single-core x86 CPU without hyper-threading, single instructions (such as increment) that do read-modify-write operations on aligned 32-bit variables are atomic even without a lock prefix. (They are not guaranteed to be, they just happen to be.) A CPU with hyper-threading behaves a lot like a CPU with two physical cores, so read-modify-write operations (other then exchange) are not guaranteed atomic without a lock prefix.

It's an irrelevant distinction now since the vast majority of CPUs your software will encounter will have more than one core one way or another. So even single-instruction read-modify-write operations on aligned 32-bit values will not be atomic unless locked. (Exchange being the exception, since it's locked even without the prefix.)

OTHER TIPS

Aligned read or write is always atomic, even on multi-CPU systems. What is not atomic (and this is probably what you have heard - or at least what you should have heard) are read-modify-write instructions, like increment, or add with a memory target. Those are not atomic even on HT systems, but they are atomic (not by guarantee, rather by chance) on systems with a single logical CPU even when not locked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top