What is the problem with writing singleton in multithreaded enviroment

Discussion in 'Java' 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

    EJP Guest

    Ronen Yacov wrote:

    > When declaring a singleton, we write:
    >
    > static CMySingle::instance()
    > {
    > static CMySingle instance;
    > return instance;
    > }


    Not in Java we don't. Are you in the right newsgroup?
     
    EJP, Jan 14, 2007
    #2
    1. Advertising

  3. Ronen Yacov wrote:
    > 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?


    That code is C++ not Java.

    But anyway. The problem is that the C++ standard mandates that
    the constructor can not be called before the first call to
    the method. This makes it a non atomic operation.

    For some details see
    http://blogs.msdn.com/oldnewthing/archive/2004/03/08/85901.aspx !

    And double locking does not solve anything. Not in Java, not in C#
    and not in C++.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jan 14, 2007
    #3
  4. Ronen Yacov

    Tom Hawtin Guest

    Arne Vajhøj wrote:
    >
    > And double locking does not solve anything. Not in Java, not in C#
    > and not in C++.


    Double-checked locking? Works fine in Java (1.5+, and in practice 1.4+),
    so long as you do it right.

    Tom Hawtin
     
    Tom Hawtin, Jan 14, 2007
    #4
  5. Tom Hawtin wrote:
    > Arne Vajhøj wrote:
    >> And double locking does not solve anything. Not in Java, not in C#
    >> and not in C++.

    >
    > Double-checked locking? Works fine in Java (1.5+, and in practice 1.4+),
    > so long as you do it right.


    It does not.

    It only works in the modified form with the volatile keyword.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jan 14, 2007
    #5
  6. Ronen Yacov

    Tom Hawtin Guest

    Arne Vajhøj wrote:
    > Tom Hawtin wrote:
    >> Arne Vajhøj wrote:
    >>> And double locking does not solve anything. Not in Java, not in C#
    >>> and not in C++.

    >>
    >> Double-checked locking? Works fine in Java (1.5+, and in practice
    >> 1.4+), so long as you do it right.

    >
    > It does not.


    Does so.

    > It only works in the modified form with the volatile keyword.


    Yes, correctly implemented it works correctly...

    Tom Hawtin
     
    Tom Hawtin, Jan 14, 2007
    #6
  7. Tom Hawtin wrote:
    > Arne Vajhøj wrote:
    >> Tom Hawtin wrote:
    >>> Arne Vajhøj wrote:
    >>>> And double locking does not solve anything. Not in Java, not in C#
    >>>> and not in C++.
    >>>
    >>> Double-checked locking? Works fine in Java (1.5+, and in practice
    >>> 1.4+), so long as you do it right.

    >>
    >> It does not.

    >
    > Does so.
    >
    >> It only works in the modified form with the volatile keyword.

    >
    > Yes, correctly implemented it works correctly...


    It is no longer the original double checked locking.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jan 14, 2007
    #7
  8. Ronen Yacov

    Lew Guest

    Arne Vajhøj wrote:
    >> And double locking does not solve anything. Not in Java, not in C#
    >> and not in C++.


    Tom Hawtin wrote:
    > Double-checked locking? Works fine in Java (1.5+, and in practice
    > 1.4+), so long as you do it right.


    >> It does not.


    > Does so.


    >> It only works in the modified form with the volatile keyword


    > Yes, correctly implemented it works correctly...


    Time out. You guys agree - double-checked locking works, if all restrictions
    and limitations are observed. Not if used "by itself", which some might call
    "incorrectly", but if used "modified, with volatile", which some might call
    "correctly".

    - Lew "the Peacemaker"
     
    Lew, Jan 14, 2007
    #8
    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:
    403
    vegetax
    May 3, 2005
  2. vegetax
    Replies:
    1
    Views:
    393
    Scott David Daniels
    May 3, 2005
  3. Ronen Yacov
    Replies:
    3
    Views:
    354
  4. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

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

Share This Page