class Singleton { public: static volatile Singleton* volatile instance(); ... private: // one more volatile added static volatile Singleton* volatile pInstance; }; // from the implementation file volatile Singleton* volatile Singleton::pInstance = 0; volatile Singleton* volatile Singleton::instance() { if (pInstance == 0) { Lock lock; if (pInstance == 0) { // one more volatile added volatile Singleton* volatile temp = new volatile Singleton; pInstance = temp; } } return pInstance; }
Example 9: A glorified version of Singleton.