关于加解锁的两种实现方式
方案一:使用C++11 atomic_flag原子特性实现细粒度加解锁
#ifndef YAGER_MUTEX_H #define YAGER_MUTEX_H #include <atomic> class CYagerMutex { public: CYagerMutex() : m_atomicFlag(ATOMIC_FLAG_INIT) {} ~CYagerMutex() {} void Lock() { while (m_atomicFlag.test_and_set()); } void UnLock() { m_atomicFlag.clear(); } private: std::atomic_flag m_atomicFlag; }; #endif //YAGER_MUTEX_H
方案二:使用libuv的mutex,实现粗粒度的加解锁
#include "libuv/include/uv.h" class CYagerMutex { public: CYagerMutex() { uv_mutex_init(&m_mutex); } ~CYagerMutex() { uv_mutex_destroy(&m_mutex); } void Lock() { uv_mutex_lock(&m_mutex); } void UnLock() { uv_mutex_unlock(&m_mutex); } private: uv_mutex_t m_mutex; };
关于加解锁的两种实现方式
方案一:使用C++11 atomic_flag原子特性实现细粒度加解锁
#ifndef YAGER_MUTEX_H #define YAGER_MUTEX_H #include <atomic> class CYagerMutex { public: CYagerMutex() : m_atomicFlag(ATOMIC_FLAG_INIT) {} ~CYagerMutex() {} void Lock() { while (m_atomicFlag.test_and_set()); } void UnLock() { m_atomicFlag.clear(); } private: std::atomic_flag m_atomicFlag; }; #endif //YAGER_MUTEX_H
方案二:使用libuv的mutex,实现粗粒度的加解锁
#include "libuv/include/uv.h" class CYagerMutex { public: CYagerMutex() { uv_mutex_init(&m_mutex); } ~CYagerMutex() { uv_mutex_destroy(&m_mutex); } void Lock() { uv_mutex_lock(&m_mutex); } void UnLock() { uv_mutex_unlock(&m_mutex); } private: uv_mutex_t m_mutex; };