Singleton initialization DCLP

M

MikeWhy

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;
}
};
 
J

Joshua Maurice

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top