devX Singleton threadsafety

Discussion in 'C++' started by Capstar, Oct 7, 2004.

  1. Capstar

    Capstar Guest

    Hi NG,

    I was reading about Singletons on devX.
    http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp

    This is their class:
    class Singleton
    {
    public:
    static Singleton* Instance();
    protected:
    Singleton();
    Singleton(const Singleton&);
    Singleton& operator= (const Singleton&);
    private:
    static Singleton* pinstance;
    };

    This is their Instance() implementation:
    Singleton* Singleton::Instance ()
    {
    if (pinstance == 0) // is it the first call?
    {
    pinstance = new Singleton; // create sole instance
    }
    return pinstance; // address of sole instance
    }

    And later they give an optimised singlethreaded version:
    Singleton* Singleton::Instance ()
    {
    static Singleton inst;
    return &inst;
    }

    They say the first one is threadsafe because new is threadsafe, and
    second isn't threid safe.

    I think the second isn't threadsafe because the constructor can be
    executed by multiple threids simultainiously, which is not possible in
    the first version.
    But I don't believe the first one is thread safe because the OS can
    interrupt this thread right after the "if (pinstance == 0)" check, end
    resume an other thread, which might be doing the same.
    So if this second thread does the test it still sees that pinstance
    equals 0, and makes a new Singleton. But when the first thread is
    resumed, it will also make a new Singleton because it still assumes that
    pinstance equals 0. So pinstance is replaced by the new Singleton, and
    the pointer to the previous one is overwritten, resulting in at least a
    memory leak.

    Is this correct, or am I missing something?

    Mark

    --
    <<Remove the del for email>>
    Capstar, Oct 7, 2004
    #1
    1. Advertising

  2. > But I don't believe the first one is thread safe because the OS can
    > interrupt this thread right after the "if (pinstance == 0)" check,
    > end resume an other thread, which might be doing the same.


    I think so, too. Thead safeness is OS specific. There is _no_ C++ way
    to make something thread safe, as there is no threads in C++.

    > So if this second thread does the test it still sees that pinstance
    > equals 0, and makes a new Singleton. But when the first thread is
    > resumed, it will also make a new Singleton because it still assumes
    > that pinstance equals 0. So pinstance is replaced by the new
    > Singleton, and the pointer to the previous one is overwritten,
    > resulting in at least a memory leak.


    I think they will delete it in the D'tor. Otherwise, yes it'll leak.
    -Gernot
    Gernot Frisch, Oct 7, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. john smith
    Replies:
    1
    Views:
    287
  2. Claudio Grondi
    Replies:
    2
    Views:
    615
    Satchidanand Haridas
    Jan 25, 2005
  3. infidel

    threadsafety in cherrypy with kid

    infidel, Aug 18, 2005, in forum: Python
    Replies:
    0
    Views:
    290
    infidel
    Aug 18, 2005
  4. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,246
    Proton Projects - Moin
    Mar 27, 2007
  5. Darren Dale

    WeakValueDict and threadsafety

    Darren Dale, Dec 10, 2011, in forum: Python
    Replies:
    3
    Views:
    205
    Darren Dale
    Dec 10, 2011
Loading...

Share This Page