[concurrency-interest] Simulating effects of final fields with memory fences

Martin Buchholz martinrb at google.com
Mon Jun 18 09:41:13 EDT 2018


Programming concurrency is hard even when you have sequential consistency
as provided by volatile or locks.
If you retreat from that for performance, then go to release paired with
acquire, so either VarHandle.releaseFence() and VarHandleAcquireFence() or
preferably VarHandle.setRelease() paired with VarHandle.getAcquire(). Going
further to storeStoreFence or loadLoadFence is too hard to get right.

On Mon, Jun 18, 2018 at 4:28 AM, Doug Lea via Concurrency-interest <
concurrency-interest at cs.oswego.edu> wrote:

> On 06/18/2018 06:12 AM, Peter Levart via Concurrency-interest wrote:
> > Hello,
> >
> > I would like to ask the list one concurrency related question. Say you
> > have a class with a final reference field:
> >
> > public class X {
> >     private final Y y;
> >
> >     public X() {
> >         y = ...;
> >     }
> >
> >     public void m() {
> >         ... use y ...
> >     }
> > }
> >
> > And that Y is either an immutable or properly synchronized type so "use
> > y" never has any data races.
> >
> > Objects of type X can be published via data races, but final modifier on
> > field y guarantees that the field will never be observed as
> > uninitialized (null) in method m.
> >
> > Now say that class X is modified in the following way:
> >
> > public class X {
> >     private Y y;
> >
> >     public X() {
> >         y = ...;
> >         VarHandle.storeStoreFence();
> >     }
> >
> >     public void m() {
> >         VarHandle.loadLoadFence();
> >         ... use y ...
> >     }
> > }
>
> In the most general case, you should use VarHandle.releaseFence()
> instead of storeStoreFence. See the discussion of Mixed Modes and
> Specializations for Release/Acquire (RA) mode in
> http://gee.cs.oswego.edu/dl/html/j9mm.html
>
> -Doug
>
> >
> > - final modifier is removed, storeStoreFence is added as the final
> > action in constructor, loadLoadFence is added as the first action in
> > method.
> >
> > Would every guarantee that holds for original class X still hold for
> > modified class X ?
>
>
>
> _______________________________________________
> 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/20180618/f3073e2f/attachment.html>


More information about the Concurrency-interest mailing list