好的源碼勝過千教萬學(xué)……
#pragma once
#include "EsPrerequisites.h"
namespace Es
{
// 鎖定義
class VirtualLock
{
public:
VirtualLock(){}
virtual ~VirtualLock(){}
virtual void Lock(void) = 0;
virtual bool TryLock(void) = 0;
virtual void UnLock(void) = 0;
};
// 自動(dòng)鎖
class AutoLock
{
public:
AutoLock(VirtualLock *pkLock)
{
m_pkLock = pkLock;
m_pkLock->Lock();
}
~AutoLock()
{
m_pkLock->UnLock();
}
protected:
VirtualLock* m_pkLock;
};
#ifdef _WIN32
// 手動(dòng)鎖
class NormalLock : public VirtualLock
{
public:
NormalLock()
{
InitializeCriticalSection(&m_kSection);
}
virtual ~NormalLock()
{
DeleteCriticalSection(&m_kSection);
}
virtual void Lock(void)
{
EnterCriticalSection(&m_kSection);
}
virtual bool TryLock(void)
{
return (TryEnterCriticalSection(&m_kSection) > 0);
}
virtual void UnLock(void)
{
LeaveCriticalSection(&m_kSection);
}
protected:
CRITICAL_SECTION m_kSection;
};
#else
#if defined( _VISION_IOS) || defined(_VISION_ANDROID)
// 手動(dòng)鎖
class NormalLock : public VirtualLock
{
public:
NormalLock()
{
pthread_mutexattr_t kAttr;
pthread_mutexattr_init(&kAttr);
pthread_mutexattr_settype(&kAttr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&m_kMutex,&kAttr);
}
virtual ~NormalLock()
{
pthread_mutex_destroy(&m_kMutex);
}
virtual void Lock(void)
{
pthread_mutex_lock(&m_kMutex);
}
virtual void UnLock(void)
{
pthread_mutex_unlock(&m_kMutex);
}
virtual bool TryLock(void)
{
return (pthread_mutex_trylock(&m_kMutex) != 0);
}
protected:
pthread_mutex_t m_kMutex;
};
#else
class NormalLock : public VirtualLock
{
public:
NormalLock()
{
pthread_mutexattr_t kAttr;
pthread_mutexattr_init(&kAttr);
pthread_mutexattr_settype(&kAttr, PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutex_init(&m_kMutex, &kAttr);
}
virtual ~NormalLock()
{
pthread_mutex_destroy(&m_kMutex);
}
virtual void Lock(void)
{
pthread_mutex_lock(&m_kMutex);
}
virtual void UnLock(void)
{
pthread_mutex_unlock(&m_kMutex);
}
virtual bool TryLock(void)
{
return (pthread_mutex_trylock(&m_kMutex) != 0);
}
protected:
pthread_mutex_t m_kMutex;
};
#endif
#endif
} // namespace Es
使用例
// define
protected:
Es::NormalLock m_kGenIDLock;
// use
...
{
Es::AutoLock kLock(&m_kGenIDLock);
// logic here
...
}