title: Is this a safe version of double-checked locking?
body: <p>Here's an idea I just came up with for a safe, efficient way of handling Singleton synchronization issues.  It's basically double-checked locking, but with a twist that involves thread-local storage.  In Java/C#/D-style pseudocode, assuming <code>__thread</code> denotes thread-local storage for static variables:</p>
  
  <pre><code>class MySingleton {
      __thread static bool isInitialized; 
      static MySingleton instance; 
  
      static MySingleton getInstance() {
          if(!isInitialized) {
              synchronized {
                  isInitialized = true;
                  if(instance is null) {
                      instance = new MySingleton();
                  }
              }
          }
  
          return instance;
     }
  }
  </code></pre>
  
  <p>This is guaranteed to only enter the <code>synchronized</code> block once per thread throughout the entire life of the program.  From that point forward, we get a simple check of a thread-local bool to see whether we've already entered the synchronized block and verified that the object is initialized from this thread.    </p>