What is the problem with writing singleton in multithreaded enviroment

Discussion in 'C++' started by Ronen Yacov, Jan 14, 2007.

  1. Ronen Yacov

    Ronen Yacov Guest

    Hi everybody,

    When declaring a singleton, we write:

    static CMySingle::instance()
    {
    static CMySingle instance;
    return instance;
    }

    Why should there be a problem in a multithreaded enviroment?
    If one thread creates a static object the other threads will have the
    same
    instance (becuase its static).

    And if there is a problem, how the docuble check locking solves it?
     
    Ronen Yacov, Jan 14, 2007
    #1
    1. Advertising

  2. Ronen Yacov

    Milind Guest

    What problem are you anticipating?? Is there something specific that
    you are looking for. We use implementation which is pretty similar to
    what you have quoted and it works fine with around 40 threads running
    in the system.
    Kindly, give more details.

    ~M
    Ronen Yacov wrote:
    > Hi everybody,
    >
    > When declaring a singleton, we write:
    >
    > static CMySingle::instance()
    > {
    > static CMySingle instance;
    > return instance;
    > }
    >
    > Why should there be a problem in a multithreaded enviroment?
    > If one thread creates a static object the other threads will have the
    > same
    > instance (becuase its static).
    >
    > And if there is a problem, how the docuble check locking solves it?
     
    Milind, Jan 14, 2007
    #2
    1. Advertising

  3. Ronen Yacov

    Guest

    I did a little search of old group threads, and this popped up:
    http://groups.google.com/group/comp...bbe602cde9d/fc1b524310a37e9e#fc1b524310a37e9e

    The jist of it is, apparently, as the thread starter discovered, the
    static construction of an object isn't an atomic operation. When it
    gets compiled, it is turned into a set of instructions where a flag is
    checked before the object is instantiated (which makes sense...I don't
    know if the C++ Standard mentions whether it has to be done in the
    certain way...I should look it up...). So if you think about it,
    multiple threads could get past this check to instantiate the object,
    which makes this not thread safe.


    Ronen Yacov wrote:
    > Hi everybody,
    >
    > When declaring a singleton, we write:
    >
    > static CMySingle::instance()
    > {
    > static CMySingle instance;
    > return instance;
    > }
    >
    > Why should there be a problem in a multithreaded enviroment?
    > If one thread creates a static object the other threads will have the
    > same
    > instance (becuase its static).
    >
    > And if there is a problem, how the docuble check locking solves it?
     
    , Jan 14, 2007
    #3
  4. Ronen Yacov

    Guest

    Milind wrote:
    > What problem are you anticipating?? Is there something specific that
    > you are looking for. We use implementation which is pretty similar to
    > what you have quoted and it works fine with around 40 threads running
    > in the system.
    > Kindly, give more details.


    Your implementation will break on a SMP box. The correct (using
    boost::call_once) is:

    struct T
    {
    static T const & instance()
    {
    boost::call_once(&call_once,once_flag);
    return get_instance();
    }
    private:
    static boost::eek:nce_flag once_flag;
    static T const & get_instance()
    {
    static T theT;
    return theT;
    }

    static void call_once()
    {
    get_instance();
    }

    };
     
    , Jan 15, 2007
    #4
    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. vegetax
    Replies:
    5
    Views:
    401
    vegetax
    May 3, 2005
  2. vegetax
    Replies:
    1
    Views:
    389
    Scott David Daniels
    May 3, 2005
  3. Ronen Yacov
    Replies:
    7
    Views:
    326
  4. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,256
    Proton Projects - Moin
    Mar 27, 2007
  5. George
    Replies:
    9
    Views:
    345
    George
    Oct 27, 2008
Loading...

Share This Page