[concurrency-interest] Happens Before guarantee in case of unsafe.putOrderedObject and corresponding nomal read of a variable

vikas vikas.vksingh at gmail.com
Thu Sep 4 22:17:42 EDT 2014

I just wanted  to know if there is any kind of synchronization/happens before
guarantees with 
unsafe.putOrderedObject(data) and normal read of that particular data.

I am aware of the guarantees of the putOrderedObject (lazySet) in
AtomicInteger. Here i wanted to  know if can we reason on any gaurantees
/happens before edge between lazySet of a variable by thread T1  and *normal
read of that variable by thread T2.*

As far as i know you need both write and read barrier  to establish some
type of ordering relationship between two threads.

For example see below: 

class DS {
         private Integer [] data;
         public Test(int m) {
           data = new Object[m];

         public void set(int index, Integer obj) {
             unsafe*.putOrderedObject(data, rawIndex(index), obj*);  // kind
of lazy set
         public Integer get(final int index) {
            return  data[index]; // *non volatile read i.e normal read*

So Suppose : 
  T1 Thread : 

    Integer v1 = new Integer(); // init the integer to  0
    ds.set(2, v1);
  T2 thread :
       Integer v1 = ds.get(2);
        if(v1 != null ) { // *normal read *
           sysout(v1.get()) ;  //* Is thread 2 always guaranteed to  see 5 *
*It would be very helpful if somebody can tell me if thread  T2 is always
guaranteed to see  5 or not*

View this message in context: http://jsr166-concurrency.10961.n7.nabble.com/Happens-Before-guarantee-in-case-of-unsafe-putOrderedObject-and-corresponding-nomal-read-of-a-variabe-tp11255.html
Sent from the JSR166 Concurrency mailing list archive at Nabble.com.

More information about the Concurrency-interest mailing list