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

Doug Lea dl at cs.oswego.edu
Mon Jun 18 07:28:37 EDT 2018


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 ?





More information about the Concurrency-interest mailing list