[concurrency-interest] DCL using Fence Intrinsics

vikas vikas.vksingh at gmail.com
Thu Mar 12 18:10:18 EDT 2015


  I am trying to understand the fence intrinsic api.
  Pershing has showw how to write DCL in C++ in his blog

  I was trying to have a similar thing in Java (*Code1*) 
     sun.misc.Unsafe U;
     Singleton instance = null

     Singleton getInstance() {
          Singleton tmp = instance;
         * U.loadFence();*
          if(tmp == null) {
              synchronized(Singleton.class) {
                   tmp = instance;
                   if(tmp == null) {
                       tmp = new Singleton();
                       instance = tmp;
       return tmp;
   * Will the above Code1 works? *
    On similar lines i have another doubt. See below *Code2*.
    if * a* and *b* are normal variables with initial value 0

       T1                                                     T2
     a = 1;                                     
     unsafe.putIntOrdered(b,1);         assert(a==1); // *will always pass*
    Code2 works because putXXXOrdered and getXXXVolatile forms a happens
before edge.
    i.e. assert in Thread T2 will always pass.

    But can we say the same thing for below code (*Code3*)
       T1                                                        T2
     a = 1;                                               while(b!=1);
     unsafe.storeFence();                           unsafe.loadFence();
     b = 1;                                               assert(a==1); 

  * /What  prevents the compiler to optimize the while loop in *Code3* to an
infinte loop./*
   So does *Code3 *works? If not, then is there anyway we can achieve the 
   expected behavior using fences. 



View this message in context: http://jsr166-concurrency.10961.n7.nabble.com/DCL-using-Fence-Intrinsics-tp12420.html
Sent from the JSR166 Concurrency mailing list archive at Nabble.com.

More information about the Concurrency-interest mailing list