MutexLock
私有成员:
Phread_mutex_t mutex_; //互斥变量
pid_t holder; //用来表示给互斥量上锁线程的tid
MutexLock()
~MutexLock()
isLockByThisThread() -> bool 用来检查给互斥量上锁的是否为当前线程
assertLocked()
lock() //加锁,调用assignHolder设置加锁线程ID
unlock() //解锁,调用unassignHolder将加锁线程ID清零
getPthreadMutex() -> pthread_mutex_t //返回指向类对象互斥量的指针,在类外对互斥量操作,这个主要用在条件变量
assignHolder() //上锁时给holder置数
unassignHolder() //解锁后给holder置0
类 UnassignGuard,类中由一个MutexLock对象的引用,构造函数调用unassignHolder,析构函数调用assignHolder,这个是为条件变量pthread_cond_wait()调用时设计的。调用pthread_cond_wait()会解锁MutexLock,等待条件。
MutexLockGuard
在使用mutex时,常常会忘记给mutex解锁,这时使用RAII方法。
私有成员: MutexLock& mutex_;
MutexLockGuard() //构造函数上锁
~MutexLockGuard() //析构函数解锁
Condition
私有成员:
MutexLock& mutex_; //互斥量
pthread_cond_t pcond_; //条件变量
Condition() //构造函数初始化互斥量和条件变量
~Condition() //析构函数
wait() //先将holder置0,然后解锁mutex_。
调用pthread_cond_wait(&pcond_,mutex_.getPthreadMutex_());先给互斥量解锁,然后等待条件成立,再把锁加上,这是个原子操作
notify() //唤醒一个线程
notifyAll() //唤醒所有线程
pthread_cond_wait()会阻塞当前线程,阻塞时,自动释放资源,等待被pthread_cond_sinal()或者pthread_cond_broadcast()唤醒,从wait出来后,自动加锁。
CountDownLatch
私有成员:
MutexLock mutex_; //互斥量
Condition condition_; //条件变量
int count;
CountDownLatch()
wait() //互斥量加锁,count>0时,阻塞等待
countDown() //count自减,为0时唤醒等待线程
getCount() //得到当前count值
Date
成员:
static const int kDayPerWeek = 7;
static const int kJulianDayOf1970_01_01;
struct YearMonthDay
{
int year;
int month;
int day;
};
int julianDayNumber_; //儒略日,从公元前4713年到现在的天数
Date();
Date(int year, int month, int day);
Date(int julianDayNumber);
Date(const struct tm&);
swap(Date& that); //交换that和当前存储的儒略日
toIsString() -> string; //获得转换后的年-月-日形式的字符串
yearMonthDay() -> struct YearMonthDay; //获得YearMonthDay结构体
int year(); //返回年
int month(); //返回月
int day(); //返回日
int weekDay(); //返回周几
int julianDayNumber(); //返回儒略日
关于julianDayNumber(儒略日),是从公元前4713年1月1日算起到现在的天数,计算公式内看懂,哎,抄百度吧……
Timestamp
类结构:
class Timestamp : public muduo::copyable, //可复制
public boost::less_than_comparable<Timestamp> //可做大小比较
{
public:
Timestamp();
explicit Timestamp(int64_t microSecondsSinceEpochArg);
void swap(Timestamp& that);
string toString() const;
string toFormattedString(bool showMicroseconds = true) const;
bool valid() const { return microSecondsSinceEpoch_ > 0; }
int64_t microSecondsSinceEpoch() const ;
time_t secondsSinceEpoch() const;
static Timestamp now();
static Timestamp invalid();
static Timestamp fromUnixTime(time_t t)
static Timestamp fromUnixTime(time_t t, int microseconds);
static const int kMicroSecondsPerSecond = 1000 * 1000;
private:
int64_t microSecondsSinceEpoch_; //从epoch到现在的微秒数
};
测试代码
int main()
{
Timestamp timeStamp=Timestamp::now();
printf("it's %ld microseconds from Epoch\n", timeStamp.microSecondsSinceEpoch());
//string str= timeStamp.toString();
string str = timeStamp.toFormattedString(true);
printf("string format is %s\n", str.c_str());
return 0;
}
TimeZone
方便时区之间的转换,以及时令之间的转换。
类结构:
class TimeZone : public muduo::copyable
{
public:
explicit TimeZone(const char* zonefile);
TimeZone(int eastOfUtc, const char* tzname);
TimeZone() {}
bool valid() const;
struct tm toLocalTime(time_t secondsSinceEpoch) const;
time_t fromLocalTime(const struct tm&) const;
static struct tm toUtcTime(time_t secondsSinceEpoch, bool yday = false);
static time_t fromUtcTime(const struct tm&);
static time_t fromUtcTime(int year, int month, int day,
int hour, int minute, int seconds);
struct Data;
private:
boost::shared_ptr<Data> data_;
};
测试代码
#include "TimeZone.h"
#include "Timestamp.h"
#include <time.h>
#include <iostream>
using namespace muduo;
void PrintTm(struct tm& T)
{
std::cout<<T.tm_year<<"-"<<T.tm_mon<<"-"<<T.tm_mday<<std::endl;
std::cout<<T.tm_hour<<"-"<<T.tm_min<<"-"<<T.tm_sec<<std::endl;
std::cout<<T.tm_wday<<std::endl;
}
int main()
{
Timestamp timeStamp=Timestamp::now();
struct tm T=TimeZone::toUtcTime(timeStamp.secondsSinceEpoch());
PrintTm(T);
TimeZone timeZone(8, "China");
struct tm T2=timeZone.toLocalTime(timeStamp.secondsSinceEpoch());
PrintTm(T2);
return 0;
}