Java教程

Muduo库

本文主要是介绍Muduo库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

MutexLock

  • #include <boost/noncopyable.hpp> #include <thread> #include <assert.h> class MutexLock : boost::noncopyable { public: MutexLock():holder_(0){ pthread_mutex_init(&mutex_,NULL); } ~MutexLock(){ pthread_mutex_destroy(&mutex_); } bool isLockByThisThread(){ return holder_ == pthread_self(); //原本是holder_ == CurrentThread::tid(); } void assertLocked(){ assert(isLockByThisThread()); } void lock(){ pthread_mutex_lock(&mutex_); holder_ = pthread_self(); //如果两行反了,当前线程还没拿上锁呢,isLockByThisThread就会返回错误的结果了 //原本是holder_ = CurrentThread::tid(); } void unlock(){ holder_ = 0; pthread_mutex_unlock(&mutex_); //同上 } pthread_mutex_t* getPhreadMutex(){ return &mutex_; } private: pthread_mutex_t mutex_; pid_t holder_; }; class MutexLockGuard : boost::noncopyable { public: explicit MutexLockGuard(MutexLock& mutex):mutex_(mutex){ mutex.lock(); } ~MutexLockGuard(){ mutex_.unlock(); } private: MutexLock mutex_; };

  有报错

这篇关于Muduo库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!