by casperc · 13y ago · 0 comments
I would have thought that the order of operations would be:
1) Read value from currentPos.x and add 1. Do same for y.
2) Pass values to constructor
3) Construct the new object
3a) init values with 0
3b) assign new values to x and y
4) Assign the newly constructed object's pointer to currentPos.I thought that the problem lay in 4 where you might read a partial update of the pointer, possibly giving weird results. Is he saying that it might get assigned first and then constructed afterwards?
Yes. In some versions of java the code: x = new X() results in assigning to x a reference to a new uninitialized object X and then a call to the constructor.
Reference assignment in java is always atomic, it is guaranteed by the memory model.
The issue here is the lack of a write barrier at the point of publishing the reference to the newly constructed object, and the lack of a read barrier at the point of reading the reference to the newly constructed object. You want to stop both writes moving forward in time (the writes in the constructor happening after the write that publishes the object) and reads moving backwards in time (the reads on the other thread reading the old values of the constructed object, rather than the initialized values - may be caused by e.g. satisfying the read from per-CPU cache).
Java's volatile acts both ways; writes are write barriers and reads are read barriers.