Hi all,
I want to use a CRITICAL_SECTION in my singleton class to synchronize access by multiple threads.
I need to lock the singleton even after initialization so the double checked lock perils are not an issue for me.
What I don't unserstand is where do I need to initialize my critical section?
Can someone show me in the following code where InitilalizeCriticalSection() needs to be used?
Code:class CSingleton { private: static CRITICAL_SECTION m_cs; static CSingleton *pInstance; CSingleton(); CSingleton(const CSingleton&); CSingleton& operator=(const CSingleton&); public: static CSingleton& Instance(); }; Singleton* CSingleton::pInstance = NULL; Singleton* CSingleton::m_cs = 0; Singleton* Singleton::Instance() { EnterCriticalSection(&m_cs); // To simplify I'm not using raii guard if ( !pInstance ) pInstance = new CSingleton; // Do stuff............ LeaveCriticalSection(&m_cs); return *pInstance; }
Many thanks!!!!




Reply With Quote