其他分享
首页 > 其他分享> > 锁的封装

锁的封装

作者:互联网

锁异常的封装,继承与自定义的异常类即可。

struct YR_Lock_Exception : public YR_Exception
{
	YR_Lock_Exception(const string& buffer) : YR_Exception(buffer){}
    YR_Lock_Exception(const string& buffer, int err) : YR_Exception(buffer,err){}
    ~YR_Lock_Exception() throw() {}
};

Linux下有各种锁机制,比如互斥锁、读写锁、自旋锁等,我们可以使用模板类实现对锁的封装。

template <class T> 			//T为锁的类型
class YR_LockT 
{
public:
    //构造函数,构造时加锁
    YR_LockT(const T& mutex) : _mutex(mutex)
    {
        _mutex.lock();
        _acquired = true;
    }
    //析构函数,析构时解锁
    ~YR_LockT()
    {
        if(_acquired)
            _mutex.unlock();
    }
    //上锁,如果已经上锁,则抛出异常
    void acquire() const
    {
        if(_acquired)
            throw YR_Lock_Exception("thread has locked!");
        _mutex.lock();
        _acquired = true;
    }
    
    //尝试上锁
    bool tryAcquire() const
    {
        _acquired = _mutex.tryLock();
        return _acquired;
    }
   	//释放锁,如果没有上锁,抛出异常
    void release() const
    {
        if (!_acquired)
        {
            throw YR_Lock_Exception("thread hasn't been locked!");
        }
        _mutex.unlock();
        _acquired = false;
    }
    //是否已经上锁
    bool acquired() const
    {
        return _acquired;
    }
protected:
    //构造函数,用于锁尝试操作
    TC_LockT(const T& mutex, bool) : _mutex(mutex)
    {
        _acquired = _mutex.tryLock();
    }   
private:
    TC_LockT(const TC_LockT&);
    TC_LockT& operator=(const TC_LockT&);
protected:
    //锁对象
    const T&        _mutex;
    //是否已经上锁
    mutable bool _acquired;
};

尝试上锁类:

template <typename T>
class YR_TryLockT : public YR_LockT<T>
{
public:
    YR_TryLockT(const T& mutex) : YR_LockT<T>(mutex, true)
    {
    }
};

空锁,不做任何锁动作

class YR_EmptyMutex
{
public:
    /**
    * @brief  写锁.
    *  
    * @return int, 0 正确
    */
    int lock()  const   {return 0;}

    /**
    * @brief  解写锁
    */
    int unlock() const  {return 0;}

    /**
    * @brief  尝试解锁. 
    *  
    * @return int, 0 正确
    */
    bool trylock() const {return true;}
};

读写锁读锁模板类

template <typename T>
class YR_RW_RLockT
{
public:
    /**
     * @brief  构造函数,构造时加锁
     *
     * @param lock 锁对象
     */
    YR_RW_RLockT(T& lock)
        : _rwLock(lock),_acquired(false)
    {
        _rwLock.ReadLock();
        _acquired = true;
    }

    /**
     * @brief 析构时解锁
     */
    ~YR_RW_RLockT()
    {
        if (_acquired)
        {
            _rwLock.Unlock();
        }
    }
private:
    /**
     *锁对象
     */
    const T& _rwLock;
    /**
     * 是否已经上锁
     */
    mutable bool _acquired;

    YR_RW_RLockT(const YR_RW_RLockT&);
    YR_RW_RLockT& operator=(const YR_RW_RLockT&);
};

读写锁写锁模板类:

template <typename T>
class YR_RW_WLockT
{
public:
    /**
     * @brief  构造函数,构造时枷锁
     *
     * @param lock 锁对象
     */
    YR_RW_WLockT(T& lock)
        : _rwLock(lock),_acquired(false)
    {
        _rwLock.WriteLock();
        _acquired = true;
    }
    /**
     * @brief 析构时解锁
     */
    ~YR_RW_WLockT()
    {
        if(_acquired)
        {
            _rwLock.Unlock();
        }
    }
private:
    /**
     *锁对象
     */
    const T& _rwLock;
    /**
     * 是否已经上锁
     */
    mutable bool _acquired;

    YR_RW_WLockT(const YR_RW_WLockT&);
    YR_RW_WLockT& operator=(const YR_RW_WLockT&);
};
};

标签:RW,const,acquired,mutex,LockT,封装,YR
来源: https://blog.csdn.net/u013635579/article/details/100074929