Singleton initialization DCLP

Discussion in 'C++' started by MikeWhy, Apr 27, 2011.

  1. MikeWhy

    MikeWhy Guest

    In context of the recent discussion of lazy initialization of singletons and
    DCLP, is the behavior of the following safe, undefined, or unsafe?

    class Foo {
    ....
    protected:
    Foo();
    public:
    static Foo & Instance()
    {
    static Foo singleton;
    return singleton;
    }
    };
     
    MikeWhy, Apr 27, 2011
    #1
    1. Advertising

  2. On Apr 27, 11:35 am, "MikeWhy" <> wrote:
    > In context of the recent discussion of lazy initialization of singletons and
    > DCLP, is the behavior of the following safe, undefined, or unsafe?
    >
    > class Foo {
    > ...
    > protected:
    >   Foo();
    > public:
    >   static Foo & Instance()
    >   {
    >      static Foo singleton;
    >      return singleton;
    >   }
    > };


    For C++03 with POSIX pthreads and win32 threads implementations, this
    lazy initialization is "not thread safe". Specifically, if the first
    call to Instance() is not happens-before all other calls, aka if the
    first calls are concurrent, then you have an effective race condition,
    aka undefined behavior.
     
    Joshua Maurice, Apr 27, 2011
    #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. Roshan
    Replies:
    5
    Views:
    574
  2. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,334
    Proton Projects - Moin
    Mar 27, 2007
  3. Wilhelm
    Replies:
    1
    Views:
    185
  4. Trans
    Replies:
    12
    Views:
    304
    Robert Klemme
    Sep 14, 2007
  5. Paul McMahon
    Replies:
    3
    Views:
    221
    David A. Black
    Jun 9, 2008
Loading...

Share This Page