MutexLock:抽象基类;
ThreadMutex:线程锁封装,继承于MutexLock;
ProcessMutex:进程锁封装;
ThreadCond:线程条件量;
ProcessCond:进程条件量;
AutoLock:自动锁;
注:进程锁、进程条件量在使用前要初始化,使用完要释放。
class MutexLock
{
public:
MutexLock(){}
~MutexLock(){}
virtual void Lock() = 0;
virtual void Unlock() = 0;
private:
DISABLE_COPY_AND_ASSIGN(MutexLock);
};
class ThreadMutex : public MutexLock
{
public:
friend class ThreadCond;
ThreadMutex()
{
int rc = 0;
pthread_mutexattr_t attr;
rc = pthread_mutexattr_init(&attr); AbortAssert(rc == 0);
rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); AbortAssert(rc == 0);
rc = pthread_mutex_init(&m_Lock, &attr); AbortAssert(rc == 0);
pthread_mutexattr_destroy(&attr);
}
~ThreadMutex()
{
pthread_mutex_destroy(&m_Lock);
}
void Lock()
{
pthread_mutex_lock(&m_Lock);
}
void Unlock()
{
pthread_mutex_unlock(&m_Lock);
}
private:
pthread_mutex_t m_Lock;
DISABLE_COPY_AND_ASSIGN(ThreadMutex);
};
class ProcessMutex : public MutexLock
{
public:
friend class ProcessCond;
ProcessMutex(pthread_mutex_t *lock):m_Lock(lock)
{
}
~ProcessMutex()
{
}
void Init()
{
int rc = 0;
pthread_mutexattr_t attr;
rc = pthread_mutexattr_init(&attr); AbortAssert(rc == 0);
rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); AbortAssert(rc == 0);
rc = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); AbortAssert(rc == 0);
rc = pthread_mutex_init(m_Lock, &attr); AbortAssert(rc == 0);
pthread_mutexattr_destroy(&attr);
}
void Destory()
{
pthread_mutex_destroy(m_Lock);
}
void Lock()
{
pthread_mutex_lock(m_Lock);
}
void Unlock()
{
pthread_mutex_unlock(m_Lock);
}
private:
pthread_mutex_t *m_Lock;
DISABLE_COPY_AND_ASSIGN(ProcessMutex);
};
class ThreadCond : public MutexLock
{
public:
ThreadCond()
{
int rc = 0;
rc = pthread_cond_init(&m_Cond, NULL); AbortAssert(rc == 0);
}
~ThreadCond()
{
pthread_cond_destroy(&m_Cond);
}
void Lock()
{
m_Lock.Lock();
}
void Unlock()
{
m_Lock.Unlock();
}
void Wait()
{
pthread_cond_wait(&m_Cond, &m_Lock.m_Lock);
}
void Signal()
{
pthread_cond_signal(&m_Cond);
}
void Broadcast()
{
pthread_cond_broadcast(&m_Cond);
}
private:
pthread_cond_t m_Cond;
ThreadMutex m_Lock;
DISABLE_COPY_AND_ASSIGN(ThreadCond);
};
class ProcessCond : public MutexLock
{
public:
ProcessCond(pthread_cond_t *cond, pthread_mutex_t *lock):m_Cond(cond), m_Lock(lock)
{
}
~ProcessCond()
{
}
void Init()
{
int rc = 0;
pthread_condattr_t attr;
rc = pthread_condattr_init(&attr); AbortAssert(rc == 0);
rc = pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); AbortAssert(rc == 0);
rc = pthread_cond_init(m_Cond, &attr); AbortAssert(rc == 0);
pthread_condattr_destroy(&attr);
m_Lock.Init();
}
void Destory()
{
pthread_cond_destroy(m_Cond);
m_Lock.Destory();
}
void Lock()
{
m_Lock.Lock();
}
void Unlock()
{
m_Lock.Unlock();
}
void Wait()
{
pthread_cond_wait(m_Cond, m_Lock.m_Lock);
}
void Signal()
{
pthread_cond_signal(m_Cond);
}
void Broadcast()
{
pthread_cond_broadcast(m_Cond);
}
private:
pthread_cond_t *m_Cond;
ProcessMutex m_Lock;
DISABLE_COPY_AND_ASSIGN(ProcessCond);
};
class AutoLock
{
public:
AutoLock(MutexLock &lock): m_Lock(lock)
{
m_Lock.Lock();
}
~AutoLock()
{
m_Lock.Unlock();
}
private:
MutexLock &m_Lock;
DISABLE_COPY_AND_ASSIGN(AutoLock);
};