easiest way to force a write membar in jdk7 and 8

Andy Nuss andrew_nuss at yahoo.com
Sun Feb 22 16:11:30 UTC 2015


JDK7 and 8 only.
I was hoping that this scheme would work regardless of "how" the IntArray obtained from the publish() method was in turn itself passed to other threads.  Because I want to do this in a library.
What do you mean by false sharing?  Does my scheme not work?  If not, is there a scheme that does work?  In my mind, doesn't the JSR 133 indicate that this *must* work?
 

     On Sunday, February 22, 2015 8:03 AM, Vitaly Davidovich <vitalyd at gmail.com> wrote:
   

 What version of java is this for? How are you publishing to other threads exactly?By the way, you should keep in that you'll most likely get false sharing in the scheme you described.  Seeing "ar" in another thread is fine so long as it doesn't try to read beyond the available range.sent from my phoneOn Feb 22, 2015 10:53 AM, "Andy Nuss" <andrew_nuss at yahoo.com> wrote:

Hi,
I tried posting a related question on the concurrency-interest mailing list but for some reason my messages are bouncing there but not here.
Basically, the idea is that some thread has been working with an int[] and filling it with values.  From some range such as [0,8) of the array, that thread is sure never again to change those values.  Now it wishes to publish the int[] range in a threadsafe way in some new wrapper object.
It cannot construct simply this (I believe), especially if somehow another thread has seen the "ar":
class IntArray {    final int[] ar;    final int from;    final int to;    IntArray (int[] ar, int from, int to) {        this.ar = ar; this.from = from; this.to = to;
    }
}
Would it work to use this:
class IntArrayVolatile {    volatile int[] ar;    final int from;    final int to;    IntArrayVolatile (int[] ar, int from, int to) {        this.ar = ar; this.from = from; this.to = to;
    }
    IntArray publish () {        return new IntArray(ar, from, to);
    }}
and then in the thread do this:   int[] ar = new int[100];   ... fill values from [0, 8) never again to change
   IntArray publishar = new IntArrayVolatile(ar, from, to).publish();   ... give publishar to other threads   ... keep filling values from position 8 and higher in "ar" and publishing further ranges in a similar way

Obviously, this is alot about the internal workings of hotspot with the membars for volatile variables.
Andy



   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20150222/bca7adca/attachment.html>


More information about the hotspot-compiler-dev mailing list