Yes, it's thread-safe.  Static constructors have an implicit lock (they
can only be called once, so other threads must be locked out until the
constructor is complete, should it currently be running).

You've essentially implemented a singleton...

-- Peter

On Sun, 8 Jul 2007 18:00:19 -0500, Ron Young <[EMAIL PROTECTED]>
wrote:

>Say I have this class, I'm copying/pasting from Visual Studio:
>
>    class StaticObject_VariableHolder
>    {
>        private static object _sharedVariable;
>        private static object _syncRoot = new object();
>
>        private StaticObject_VariableHolder()
>        {
>        }
>
>        public static object SharedVariable
>        {
>            get
>            {
>                if (_sharedVariable == null)
>                {
>                    lock (_syncRoot)
>                    {
>                        if (_sharedVariable == null)
>                        {
>                            _sharedVariable = new object();
>                        }
>                    }
>                }
>
>                return _sharedVariable;
>            }
>        }
>    }
>
>And I read this article,
>http://msdn.microsoft.com/msdnmag/issues/05/08/Concurrency/
>
>Is the following equivalent as above in regards to a one-time
initialization
>of a shared variable that is publicly (statically) accessible:
>
>
>
>    /// <summary>
>    /// From http://msdn.microsoft.com/msdnmag/issues/05/08/Concurrency/
>    /// on "What Memory Needs Lock Protection"
>    /// "Second; memory that is read-only after publication does not need
a
>lock
>    /// because any invariants associated with it must hold for the
program
>    /// (since the value does not change).
>    /// </summary>
>    /// <typeparam name="T"></typeparam>
>    class BaseProvider<T>
>    {
>        private static readonly T _sharedObject;
>
>        public static T SharedObject { get { return _sharedObject; } }
>
>        static BaseProvider()
>        {
>            _sharedObject = default(T);
>        }
>    }
>
>It's not that we need BaseProvider to be singleton - although it could
be -
>but just access to it's shared variable.
>

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to