Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1432 connectés 

 


 Mot :   Pseudo :  
 
 Page :   1  2
Page Suivante
Auteur Sujet :

[c++] class singleton

n°898417
xterminhat​e
Si vis pacem, para bellum.
Posté le 14-11-2004 à 20:53:04  profilanswer
 

Reprise du message précédent :
Ce modèle ci ne convenait pas ?

Code :
  1. template< typename X >
  2. struct singleton 
  3. static X& instancier()
  4. {
  5.  static X obj;
  6.  return obj;
  7. }
  8. virtual ~singleton() {}
  9. protected:
  10. singleton() {}
  11. singleton( const singleton& ) {}
  12. singleton& operator=( const singleton& ) {}
  13. };


---------------
Cordialement, Xterm-in'Hate...
mood
Publicité
Posté le 14-11-2004 à 20:53:04  profilanswer
 

n°898787
tanguy
Posté le 15-11-2004 à 11:14:50  profilanswer
 

> Ce modèle ci ne convenait pas ?
 
euh si ? :D
 
En fait je comprends pas pourquoi tout le monde utilise un pointeur pour faire cela au lieu d'un static.

n°898794
Lam's
Profil: bas.
Posté le 15-11-2004 à 11:19:42  profilanswer
 

tanguy a écrit :

> Ce modèle ci ne convenait pas ?
 
euh si ? :D
 
En fait je comprends pas pourquoi tout le monde utilise un pointeur pour faire cela au lieu d'un static.


 
Parce que dans le bouquin du GoF, c'est un pointeur qui est utilisé. :o  
Mais comme déjà dit, cette solution a des problèmes si tu as beaucoup de données statiques qui sont construites avant que le main() ne soit exécuté.

n°898820
tanguy
Posté le 15-11-2004 à 11:43:03  profilanswer
 

> cette solution a des problèmes si tu as beaucoup de données
> statiques qui sont construites avant que le main() ne soit
> exécuté.  
 
Justement il n'y a pas de problemes puisque le static n'est fait que si l'on appelle la methode en question. Donc c'est une lazy initialization.

n°898844
Lam's
Profil: bas.
Posté le 15-11-2004 à 12:02:25  profilanswer
 

tanguy a écrit :

> cette solution a des problèmes si tu as beaucoup de données
> statiques qui sont construites avant que le main() ne soit
> exécuté.  
 
Justement il n'y a pas de problemes puisque le static n'est fait que si l'on appelle la methode en question. Donc c'est une lazy initialization.


 
 
Vi, mais tu me quotes de façon pernicieuse. Cette solution => La solution a base de membre, pas celle à base d'objet statique dans une fonction.  
 
Donc on est d'accord, la méthode GoF n'est pas la meilleure. Par contre, c'est quoi une lazy "initialization". C'est comme une initialisation ?  :D  
 

n°899074
el muchach​o
Comfortably Numb
Posté le 15-11-2004 à 15:33:10  profilanswer
 

tanguy a écrit :

> Ce modèle ci ne convenait pas ?
 
euh si ? :D
 
En fait je comprends pas pourquoi tout le monde utilise un pointeur pour faire cela au lieu d'un static.


 
lusage du static local est d'ailleurs connu sous le nom de Meyers Singleton (de Scott Meyers).
Problème : il n'est pas thread-safe.
Solution : double-check pattern.
 
Un article complet la-dessus.
http://www.research.ibm.com/design [...] -jun96.txt
 

Citation :

Long ago, in a galaxy far, far away, Scott Meyers[endnote1] posited the
following:
 
    My [version of Singleton] is quite similar to yours, but instead
    of using a class static and having Instance return a pointer, I
    use a function static and return a reference:
 
        Singleton& Singleton::Instance () {
            static Singleton s;
            return s;
        }
 
    This seems to offer every advantage your solution does (no
    construction if never used, no dependence on initialization order
    between translation units, etc.), plus it allows users to use
    object syntax instead of pointer syntax.  Furthermore, my solution
    makes it much less likely a caller will inadvertently delete the
    singleton in a misguided attempt to avoid a memory leak.
 
    Am I overlooking a reason for returning a pointer to a class
    static instead of a reference to a function static?
 
The only drawback I could see here is that it makes it hard to extend
the Singleton through subclassing, since Instance will always create
an object of type Singleton.  (For more on extending the Singleton
class, see the discussion beginning on page 130 in *Design Patterns*.)
Besides, one needn't worry about deleting the Singleton instance if
its destructor isn't public.  While I have since developed a slight
preference for returning a reference, in the end it seems to make
little difference.
 
Later, Erich Gamma[endnote2] noticed a more fundamental problem with
this approach:
 
    It turns out that it is not possible to make [Scott's approach]
    thread-safe if multiple threads can call Instance. The problem is
    that [some C++ compilers generate] some internal data structures that
    cannot be protected by locks. In such situations you would have to
    acquire the lock at the call site---pretty ugly.
 
Yep.  And it wouldn't be long before Doug Schmidt[endnote3] got
bitten by this very bug:
 
    [The Double-Check] pattern [Editor's Corner, March '96] emerged as
    I was proofreading John Vlissides' Pattern Hatching column for
    April '96.  In this column, John talks about Singleton in the
    context of protection for multi-user file systems.  Ironically,
    we'd been having some mysterious problems recently with memory
    leaks on multi-threaded versions of ACE on multi-processors.
 
    As I read John's column, it suddenly struck me that the problem
    was caused by multiply initialized Singletons due to race
    conditions.  Once I saw the connection between the two issues, and
    factored in the key forces (e.g., no locking overhead for normal
    use of the Singleton), the solution jumped right out.
 
About a month later[endnote4], Doug sent me a follow-up:
 
    [O]ne of my grad students (Tim Harrison) recently implemented a
    C++ library class called Singleton, which basically adapts
    existing classes to become "Singletons."  We use this in ACE now,
    and it's quasi-useful.  The nice thing about it is that it
    automates the Double-Check [pattern][endnote5] and also enables easy
    parameterization of the LOCK strategy.  I've enclosed it below,
    just for fun.
 
        template <class TYPE, class LOCK>
        class Singleton {
        public:
            static TYPE* Instance();
 
        protected:
            static TYPE* _instance;
            static LOCK _lock;
        };
 
        template <class TYPE, class LOCK>
        TYPE* Singleton<TYPE, LOCK>::Instance () {
            // perform the Double-Check pattern...
 
            if (_instance == 0) {
                Guard<LOCK> monitor(_lock);
 
                if (_instance == 0) _instance = new TYPE;
            }
 
            return _instance;
        }
 
I was intrigued, especially the part about being "quasi-useful."  I
asked whether he said that because this approach doesn't really preclude
creating multiple objects of the base type (since presumably that type
is defined as a non-singleton elsewhere).  His response was
illuminating, and a little unnerving[endnote6]:
 
    Right, exactly.  Another problem is that many C++ compilers (e.g,.
    G++) don't implement static data members within templates.  In
    this case you have to implement the static instance method like
    this:
 
        template <class TYPE, class LOCK>  
        TYPE* Singleton<TYPE, LOCK>::Instance () {
            static TYPE* _instance = 0;
            static LOCK _lock;
 
            if (_instance == 0)
            // ...
 
            return _instance;
        }
 
    Ah, the joys of cross-platform C++ portability! ;-)
 
That, in turn, fired some of my dormant neurons.  I wrote back that if
you really wanted to make a class inherently singleton, you could
subclass it from this template, passing the subclass as a template
parameter (Cope's curiously recurring template pattern[endnote7]
again---I love it!).  For example:
 
    class User : public Singleton<User, Mutex> { ... }
 
That way, you would preserve Singleton semantics without recoding the
pattern in all its multi-threaded gory (sic).  
 
Caution: I haven't tried out this variation myself, nor have I had
occasion to use it.  I just think it's neat, both aesthetically and in
the way we arrived at it.  I used to think Singleton was one of the
more trivial of our patterns, hardly worthy to hobnob with the likes
of Composite, Visitor, etc.; and maybe that explains its silence on
some issues.  Boy, was I wrong!


 
 
Enfin, l'article le plus complet sur l'implémentation d'une classe Singleton
http://www.codeproject.com/cpp/singleton.asp
(article visiblement calqué sur le bouquin d'A. Alexandrescu).
 
ps : j'ai pas eu le temps de regarder dans le détail, mais dans l'article suivant, l'auteur explique que le double-check pattern n'est pas portable : http://www.cs.umd.edu/%7Epugh/java [...] cking.html


Message édité par el muchacho le 15-11-2004 à 15:51:13
mood
Publicité
Posté le   profilanswer
 

 Page :   1  2
Page Suivante

Aller à :
Ajouter une réponse
 

Sujets relatifs
[css] class, id et heritageRécupérer via Javascript une propriété d'une class CSS non utilisé
[JAVA] Inner class et accès aux membres protected --> compiler bug ?Propriétés 'class' et 'div'
Probleme de class avec GDComen séparer l'interface graphik et les méthod en diférentes class
[Applet] load : class not foundInfos sur les fichiers .class
[Java] Retrouver le code java a partir des .class ?Eclipse et Export Jar : Failed to load Main-Class ....
Plus de sujets relatifs à : [c++] class singleton


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR