* implementation of ResGuard. * See* by xiuleili 2013-7-5 */ #pragma once #include // Instances of this class will be accessed by multiple threads. So, // all members of this class (except the constructor and destructor) // must be thread-safe. class ResGuard { public: ResGuard() { m_guardcount = 0; pthread_mutex_init(&m_cs, NULL); } ~ResGuard() { pthread_mutex_destroy(&m_cs); } // IsGuarded is used for debugging bool isgurded() const { return(m_guardcount > 0); } public: class Lock { public: Lock(ResGuard& rg) : m_rg(rg) { m_rg.Guard(); }; ~Lock() { m_rg.Unguard(); } private: ResGuard& m_rg; }; private: void Guard() { pthread_mutex_lock(&m_cs); m_guardcount++; } void Unguard() { m_guardcount--; pthread_mutex_unlock(&m_cs); } // Guard/Unguard can only be accessed by the nested Guard class. friend class ResGuard::Lock; private: pthread_mutex_t m_cs; long m_guardcount; // # of EnterCriticalSection calls }; /* * implementation of ResGuard. * See * by xiuleili 2013-7-5 */ #pragma once #include // Instances of this class will be accessed by multiple threads. So, // all members of this class (except the constructor and destructor) // must be thread-safe. class ResGuard { public: ResGuard() { m_guardcount = 0; pthread_mutex_init(&m_cs, NULL); } ~ResGuard() { pthread_mutex_destroy(&m_cs); } // IsGuarded is used for debugging bool isgurded() const { return(m_guardcount > 0); } public: class Lock { public: Lock(ResGuard& rg) : m_rg(rg) { m_rg.Guard(); }; ~Lock() { m_rg.Unguard(); } private: ResGuard& m_rg;
Singleton template (二)
cs); }
// IsGuarded is used for debugging
bool isgurded() const { return(m_guardcount > 0); }
public:
class Lock {
public:
Lock(ResGuard& rg) : m_rg(rg) { m_rg.Guard(); };
~Lock() { m_rg.Unguard(); }
private:
ResGuard& m_rg;
};
private:
void Guard() { EnterCriticalSection(&m_cs); m_guardcount++; }
void Unguard() { m_guardcount--; LeaveCriticalSection(&m_cs); }
// Guard/Unguard can only be accessed by the nested CGuard class.
friend class ResGuard::Lock;
private:
CRITICAL_SECTION m_cs;
long m_guardcount; // # of EnterCriticalSection calls
};
#endif
/*
* Implementation of ResGuard on Windows.
* see <>
* by xiuleili 2013-7-5
*/
#pragma once
#ifndef _UNIX
#include
///////////////////////////////////////////////////////////////////////////////
// Instances of this class will be accessed by multiple threads. So,
// all members of this class (except the constructor and destructor)
// must be thread-safe.
class ResGuard {
public:
ResGuard() { m_guardcount = 0; InitializeCriticalSection(&m_cs); }
~ResGuard() { DeleteCriticalSection(&m_cs); }
// IsGuarded is used for debugging
bool isgurded() const { return(m_guardcount > 0); }
public:
class Lock {
public:
Lock(ResGuard& rg) : m_rg(rg) { m_rg.Guard(); };
~Lock() { m_rg.Unguard(); }
private:
ResGuard& m_rg;
};
private:
void Guard() { EnterCriticalSection(&m_cs); m_guardcount++; }
void Unguard() { m_guardcount--; LeaveCriticalSection(&m_cs); }
// Guard/Unguard can only be accessed by the nested CGuard class.
friend class ResGuard::Lock;
private:
CRITICAL_SECTION m_cs;
long m_guardcount; // # of EnterCriticalSection calls
};
#endif
Resguard_linux.hpp