The Meyer singleton isn't the best solution in general, and especially not in a multithreaded environment. A more general way of implementing a singleton would be:
class ClassA
{
static ClassA* ourInstance;
// ctor's, etc.
public:
static ClassA& instance();
};
and in the source file:
ClassA* ClassA::ourInstance = &instance();
// This can be in any source file.
ClassA&
ClassA::instance()
{
if ( ourInstance == NULL ) {
ourInstance = new ClassA;
}
return *ourInstance;
}
This is thread safe if no threads are created before entering
main
(which should be the case), and it is not dynamically
loaded (which should also be the case—if the object is to
be unique, and accessible from the constructors of static
objects, then it has to be their when the static constructors
run). It also has the advantage of avoiding any order of
destruction problems.