// singleton.h // ----------- template class CSingleton { private: CSingleton(); ~CSingleton(); protected: static volatile LONG m_lLock; public: static T* GetInstance() { // Wait for the variable to unlock and lock it while (::InterlockedExchange(&m_lLock, 1)) ::Sleep(1); // Create a single instance of the object (only if it's not yet exists) static T object; // Unlock ::InterlockedExchange(&m_lLock, 0); return &object; } };