Page 2 of 2

Re: Platform Independence

Posted: Fri Aug 08, 2008 5:29 am
by JamesM
Solar wrote:
JamesM wrote:

Code: Select all

MyClass::instance().doStuff();
compiles down to the equivalent of:

Code: Select all

MyClass myClass;
myClass.doStuff();
With -O3.
It does? I mean, instance() contains at least a check whether the singleton has been initialized. If you want to be thread-safe, that check would have to be mutexed. I don't say it's impossible to optimize that away, but...

Ah well.
Ah - it doesn't. We don't use a full-on singleton pattern - the singleton pattern is used only so that a variable can be accessed statically from anywhere. The accessor tends to look something like:

Code: Select all

static const MyClass &instance()
{
  return m_Instance;
}
Which of course, can be compiled down into nothingness. I understand your point better now, and yes there would normally be an overhead, but we've done away with this at the expense of no init checks.

James