>This question is a follow up on my earlier Singleton question...
>
>How do you make a class Thread-Safe?
>
>Would this class be both thread-safe and a singleton?
>
>
>class MyClass
>{
> private static readonly object _lockobj = new object();
>
> private static MyClass instance = null;
> public static MyClass Instance
> {
> get
> {
> if (instance == null)
> instance = new MyClass();
>
> return instance;
> }
> }
>
> public MyClass()
> {
> lock (_lockobj) ;
> }
>}
>
>
>Since the class is a singleton, and by getting a lock in the CTOR, is it now thread-safe, assuming I do
>
>
>MyClass class = MyClass.Instance.
>
>
>Thanks
I am certainly no expert on Thread Safety and I do not know all the tricks about this topic. Viv gave an excellent link to making a Singleton thread safe and I have updated my notes. There are many other aspects to thread safety and often times it deals not with a whole class but sometimes a method or a simple call to update a control. I am not sure how one actually learns Thread Safe operations other than a bit at a time as the situations come up, by keeping in mind the potential of an operation to be accessed by two threads at once.
Just my take on this topic
Tim
Timothy Bryan