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;
}
}
private static string ThreadId { get { return
Thread.CurrentThread.ManagedThreadId.ToString(); } }
}
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.
Ron
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com