// Use of this source code is governed by a BSD-style license// that can be found in the License file.//// Author: Shuo Chen (chenshuo at chenshuo dot com)#ifndef MUDUO_BASE_CONDITION_H#define MUDUO_BASE_CONDITION_H#include <muduo/base/Mutex.h>#include <boost/noncopyable.hpp>#include <pthread.h>namespacemuduo
{classCondition : boost::noncopyable
{public:
//构造函数,初始化条件变量
explicit Condition(MutexLock& mutex)
: mutex_(mutex)
{
MCHECK(pthread_cond_init(&pcond_, NULL));
}
//析构函数,释放条件变量
~Condition()
{
MCHECK(pthread_cond_destroy(&pcond_));
}
//解除锁对应的线程ID,并等待条件变量void wait()
{
MutexLock::UnassignGuard ug(mutex_);
MCHECK(pthread_cond_wait(&pcond_, mutex_.getPthreadMutex()));
}
// returns true if time out, false otherwise.//时间等待bool waitForSeconds(double seconds);
//唤醒一个等待条件变量的线程void notify()
{
MCHECK(pthread_cond_signal(&pcond_));
}
//发广播形式唤醒所有等待该环境变量的线程void notifyAll()
{
MCHECK(pthread_cond_broadcast(&pcond_));
}
private:
MutexLock& mutex_; //互斥锁
pthread_cond_t pcond_;//条件变量
};
}
#endif // MUDUO_BASE_CONDITION_H
Condition.cc
// Use of this source code is governed by a BSD-style license// that can be found in the License file.//// Author: Shuo Chen (chenshuo at chenshuo dot com)#include <muduo/base/Condition.h>#include <errno.h>// returns true if time out, false otherwise.//超过规定的时间,如果还没有接到信号,就直接返回bool muduo::Condition::waitForSeconds(double seconds)
{
struct timespec abstime;
// FIXME: use CLOCK_MONOTONIC or CLOCK_MONOTONIC_RAW to prevent time rewind.
clock_gettime(CLOCK_REALTIME, &abstime);
const int64_t kNanoSecondsPerSecond = 1e9;
int64_t nanoseconds = static_cast<int64_t>(seconds * kNanoSecondsPerSecond);
abstime.tv_sec += static_cast<time_t>((abstime.tv_nsec + nanoseconds) / kNanoSecondsPerSecond);
abstime.tv_nsec = static_cast<long>((abstime.tv_nsec + nanoseconds) % kNanoSecondsPerSecond);
MutexLock::UnassignGuard ug(mutex_);
return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getPthreadMutex(), &abstime);
}