[concurrency-interest] Volatile stores in constructors, disallowed to see the default value

Vitaly Davidovich vitalyd at gmail.com
Wed Nov 27 09:06:34 EST 2013


But the issue is whether assignment of the new C instance to some other
memory must occur after the volatile stores.  JMM and your cookbook allow
subsequent normal stores to float above prior volatile stores, which is
exactly the case of assigning the C instance.  Final field assignments
explicitly disallow this reordering, but where's the same for volatiles?

Constructors are just regular methods for this example *unless* we call
them out explicitly for this case, like finals.  Otherwise, if pretend the
constructor is just some method foo() with exact same logic, everyone
agrees that the reordering is possible.


Sent from my phone
On Nov 27, 2013 9:01 AM, "Doug Lea" <dl at cs.oswego.edu> wrote:

> On 11/27/2013 08:13 AM, Oleksandr Otenko wrote:
>  If JLS already mentions that constructors with volatile stores in them are
>> treated differently, then there is no ambiguity already.
> It doesn't, but there are common cases where the effects can be
> made identical to how final fields are treated (which is
> convenient for JVMs).
> Using only the simple cookbook approximations to JMM rules,
> including that you cannot ever reorder volatile stores with
> any other stores, plus the basic weakening transformations
> (see "Removing Barriers" section in http://gee.cs.oswego.edu/dl/
> jmm/cookbook.html), you can reason as follows for the example:
> class C {
>   volatile int x, y;
>   C(int a, int b) { x = a; y = b; }
> }
> There are no previous accesses of x or y.
> And none except for the stores within the constructor.
> And none until after the second store,
> which cannot be reordered wrt other stores.
> In this case, you don't need any fencing between the stores
> to x and y. But you still need to prevent store reorderings
> by the caller of the constructor, which can be done
> in the same way it is done for classes with final fields:
> placing a store fence at end of constructor.
> -Doug
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20131127/daa98e0b/attachment-0001.html>

More information about the Concurrency-interest mailing list