jmdavis/phobos — comment on commit 56c8acdeb45d0f10fb065b0db131b1eb9aa6ab81
File: std/datetime.d line 28545
By dsimcha:

Instead of relying on atomics or language lawyer minutiae, why not use the TLS-checked locking patterm? An example can be found in the implementation of std.parallelism.taskPool(). The idea is that you cache whether the object has been initialized or not in a thread-local variable. For example:

static bool isInitialized; // Thread local
__gshared Object mySingleton;
void getSingleton() {
if(!isInitialized) {
isInitialized = true;
synchronized {
if(mySingleton is null) {
mySingleton = new Object;
}
}
}

return mySingleton;
}

This way you don't need to fiddle with atomics, you don't need to rely on minutiae of the memory model, and you're guaranteed to never synchronize more than once per thread.