条件变量操作
功能:封装对条件变量的一系列操作
知识点:
- 条件变量常用的函数
pthread_cond_init
pthread_cond_wait
pthread_cond_signal
pthread_cond_broadcast
pthread_cond_destroy
pthread_cond_timedwait
用途:
可用于多线程条件变量的操作,可替代管道的作用
代码及分析:
Condition.h
// 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>
namespace muduo
{
class Condition : 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);
}