[concurrency-interest] concurrency puzzle

David Holmes dcholmes at optusnet.com.au
Mon Sep 11 22:34:23 EDT 2006

Hi Vijay,

> > So this means that:
> >
> >   class A {
> >       int x;
> >       ...
> >   }
> >
> >is semantically different to:
> >
> >   class A {
> >       int x = 0;
> >       ...
> >   }
> >
> >because the explicit write of zero to x could "appear" later
> (even though it
> >is overwriting a zero value in main memory) just as the value 10 could
> >"appear" later.
> >
> >Wow! That is a very subtle distinction. I'm not sure there are
> any practical
> >consequences arising from it but I find it disconcerting to have this
> >difference.
> >
> >
> The difference could be huge.

I don't claim to understand the attempted formalism of this. The difference

    int x;


    int x = 0;

is that given a thread T that is reading x from an instance that was
unsafely published, and where T first writes the value x=N, then in the
first case the legal values for the read of x is only N, while in the second
case it is 0 or N.

I guess the practical consequence is that in the second case you may
actually discover that you have a bug due to unsafe publication, while in
the first case you likely never will. Of course it would be perfectly legal
(probably not worth the effort) for the compiler to optimize away writes of
default values in the constructor, such that the runtime behaviour of both
cases is the same.

Where do you see the potential for a huge difference?

David Holmes

More information about the Concurrency-interest mailing list