您的位置:首页 > 理论基础 > 计算机网络

[Muduo网络库源码分析] (2) base/Condition.cc_h_条件变量操作

2017-07-31 09:08 429 查看

条件变量操作

功能:封装对条件变量的一系列操作

知识点:

条件变量常用的函数

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: