MutexLock头文件Mutex.h,其中有两个类MutexLock和MutexLockGuard
#ifndef MUDUO_BASE_MUTEX_H
#define MUDUO_BASE_MUTEX_H
#include <muduo/base/CurrentThread.h>
#include <boost/noncopyable.hpp>
#include <assert.h>
#include <pthread.h>
namespace muduo
{
class MutexLock : boost::noncopyable
{
public:
MutexLock() : holder_(0)
{ //构造函数
int ret = pthread_mutex_init(&mutex_, NULL); //初始化一个互斥锁
assert(ret == 0); (void) ret;
}
~MutexLock()
{ //析构函数
assert(holder_ == 0);
int ret = pthread_mutex_destroy(&mutex_); //销毁一个互斥锁
assert(ret == 0); (void) ret;
}
bool isLockedByThisThread()
{ return holder_ == CurrentThread::tid(); }
void assertLocked()
{ assert(isLockedByThisThread()); }
// internal usage
void lock()
{
pthread_mutex_lock(&mutex_); //加锁
holder_ = CurrentThread::tid();
}
void unlock()
{
holder_ = 0;
pthread_mutex_unlock(&mutex_); //解锁
}
pthread_mutex_t* getPthreadMutex() /* non-const */ //返回当前的锁
{ return &mutex_; }
private:
pthread_mutex_t mutex_;
pid_t holder_;
};
class MutexLockGuard : boost::noncopyable
{
public:
explicit MutexLockGuard(MutexLock& mutex) : mutex_(mutex)
/*RAII封装 mutex是MutexLock的引用,即MutexLockGuard这个类并不管理mutex的生存期,并不负责释放这个对象,仅仅是MutexLockGuard使用了MutexLock中的lock和unlock方法,二者只是关联关系*/
{ mutex_.lock(); }
~MutexLockGuard()
{ mutex_.unlock(); }
private:
MutexLock& mutex_;
};
}
#define MutexLockGuard(x) error "Missing guard object name"
#endif // MUDUO_BASE_MUTEX_H
Condition头文件Condition.h,条件变量,即根据条件来决定是否加锁
#ifndef MUDUO_BASE_CONDITION_H
#define MUDUO_BASE_CONDITION_H
#include <muduo/base/Mutex.h>
#include <boost/noncopyable.hpp>
#include <pthread.h>
namespace muduo
{
class Condition : boost::noncopyable
{
public:
explicit Condition(MutexLock& mutex)
: mutex_(mutex)
{ pthread_cond_init(&pcond_, NULL); } //构造函数 条件变量初始化
~Condition()
{ pthread_cond_destroy(&pcond_); } //析构函数 销毁条件变量
void wait()
{ pthread_cond_wait(&pcond_, mutex_.getPthreadMutex()); }//阻塞在条件变量上
// returns true if time out, false otherwise.
bool waitForSeconds(int seconds);
void notify()
{ pthread_cond_signal(&pcond_); } //解除在条件变量上的阻塞
void notifyAll()
{ pthread_cond_broadcast(&pcond_); } //释放阻塞的所有线程
private:
MutexLock& mutex_;
pthread_cond_t pcond_;
};
}
#endif // MUDUO_BASE_CONDITION_H
Condition.cc文件
#include <muduo/base/Condition.h>
#include <errno.h>
// returns true if time out, false otherwise.
bool muduo::Condition::waitForSeconds(int seconds)
{//等待函数的实现
struct timespec abstime;
clock_gettime(CLOCK_REALTIME, &abstime);
abstime.tv_sec += seconds;
return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getPthreadMutex(), &abstime);
}
CountDownLatch头文件CountdownLatch.h ,既可以用于所有子线程等待主线程发起 “起跑”
也可以用于主线程等待子线程初始化完毕才开始工作(有点像等待倒计时结束
#ifndef MUDUO_BASE_COUNTDOWNLATCH_H
#define MUDUO_BASE_COUNTDOWNLATCH_H
#include <muduo/base/Condition.h>
#include <muduo/base/Mutex.h>
#include <boost/noncopyable.hpp>
namespace muduo
{
class CountDownLatch : boost::noncopyable
{
public:
explicit CountDownLatch(int count);
void wait();
void countDown();
int getCount() const;
private:
mutable MutexLock mutex_;
Condition condition_;
int count_;
};
}
#endif // MUDUO_BASE_COUNTDOWNLATCH_H
CountDownLatch.cc
#include <muduo/base/CountDownLatch.h>
using namespace muduo;
CountDownLatch::CountDownLatch(int count):mutex_(), condition_(mutex_),count_(count)
{} //构造函数
void CountDownLatch::wait()
{
MutexLockGuard lock(mutex_);
while (count_ > 0) {
condition_.wait();
}
}
void CountDownLatch::countDown()
{
MutexLockGuard lock(mutex_);
--count_;
if (count_ == 0) {
condition_.notifyAll();
}
}
int CountDownLatch::getCount() const
{
MutexLockGuard lock(mutex_);
return count_;
}