Friday 27 July 2012

Write a standard lock() plus “double check” to create a critical section around a variable access.

Purpose:

  • Any seasoned developer that has written multi threaded code would be familiar with this pattern. If the developer can tie it to both deadlock and performance reasons then it is a really good indicator.

Potential Answer:
public class SingletonObject
        {
            private static volatile Singleton _instance;
            private static object lockObject = new object();

            public static Singleton Instance
            {
                get
                {
                    if (instance == null)
                    {
                        lock (lockObject)
                        {
                            if (instance == null)
                                instance = new Singleton();
                        }
                    }
                    return instance;
                }
            }
        }




No comments:

Post a Comment