// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Threading; namespace Singleton.Inst { public class MemBarrierSingleton { private static MemBarrierSingleton instance; private static readonly object mylock = new object(); private MemBarrierSingleton() { Console.WriteLine(" Contructed\t: " + (long)TimeSpan.FromTicks(DateTime.Now.Ticks).TotalMilliseconds); } public static MemBarrierSingleton Instance { get { if(instance == null) { lock(mylock) { if(instance == null) { MemBarrierSingleton temp = new MemBarrierSingleton(); Thread.MemoryBarrier(); instance = temp; } } } Console.WriteLine(" Retrieved\t: " + (long)TimeSpan.FromTicks(DateTime.Now.Ticks).TotalMilliseconds); return instance; } } public void Something() { Console.WriteLine(" Called\t: " + (long)TimeSpan.FromTicks(DateTime.Now.Ticks).TotalMilliseconds); } } }