taigetco
taigetco

Reputation: 570

Reorderings in java memory model

re-read the JMM, find that there is one sentence I do not understand:

We consider here only variables that are readable and writable as an atomic unit -- that is, no bit fields, unaligned accesses, or accesses larger than word sizes available on a platform.

Anyone can explain the cases shown above which the words are bold.

Upvotes: 4

Views: 96

Answers (1)

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

On some architectures, the processor can do in a single command multiple memory accesses:

  • bit fields: setting certain bits of a memory; this is usually implemented using read-modify-write, with separate accesses for read and write.
  • unaligned access: on some CPU architectures, the processor imposes constraints on the addresses you can use when you access multibyte values, especially if you cross word boundaries. Most modern processors do no have this issue because they can execute multiple reads to cover the required address range; if you read from 0x01 to 0x05, it gets split into a 0x01-0x04 and a 0x05 read. This is compounded if you will need to write there; the same areas will get first read, then written, doing 3 times as many memory operations as necessary when compared to an address that is aligned (the address is a multiple of 4). (keep in mind that on modern CPUs the issue is more complex, as you have a very deep memory hierarchy with different per-layer alignment constraints).

In these cases, an operation will need to cross several memory accesses, thus tehre is potential for race conditions, thus no atomicity guarantees.

Upvotes: 2

Related Questions