someone told me just to write singleton as a local class, is that true?
I am wondering why using the local class can ensure thread safety.
#include <boost/utility.hpp>
class singleton : private boost::noncopyable {
public:
static singleton& instance() {
static singleton inst;
return inst;
}
private:
singleton() = default;
~singleton() = default;
};
The construction of local static variables is guaranteed to be thread-safe.
Also, avoid singletons at all cost. They are just as terrible as globals are.
Take a look at this post: what is correspoding feature for synchronized in java?
feature-for-synchronized-in-java
Basically it states that C++ doesn't has a language level feature for locking mechanisms,
wich you need to make your singelton class threadsafe, though this articel
http://en.wikipedia.org/wiki/Double-checked_locking about the Double Checked Locking Pattern
states, that no locking is needed for singletons (an example is included for c).