Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Thread Safe Class
Message
De
11/08/2009 08:42:26
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
Information générale
Forum:
ASP.NET
Catégorie:
Conception classe
Divers
Thread ID:
01417234
Message ID:
01417294
Vues:
46
>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
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform