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,lock,unlock,mutex,pthread,holder,MutexLock 来源: https://www.cnblogs.com/dbnn/p/16641492.html