Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Thread Safe Class
Message
De
11/08/2009 08:38:11
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
 
À
11/08/2009 04:37:41
Information générale
Forum:
ASP.NET
Catégorie:
Conception classe
Divers
Thread ID:
01417234
Message ID:
01417291
Vues:
50
>>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.
>>
>>
>Hi,
>This describes various approaches (including yours):http://www.yoda.arachsys.com/csharp/singleton.html

Fantastic link Viv! I have added some of that to my notebook for reference.
Timothy Bryan
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform