Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Thread Safe Class
Message
Information générale
Forum:
ASP.NET
Catégorie:
Conception classe
Divers
Thread ID:
01417234
Message ID:
01417263
Vues:
70
>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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform