读写锁 写优先

 

//先定义一个全局锁对象
tg_rwlock g_rwlock;

void test()
{
    {
        tg_rwlock::read_guard(g_rwlock);

        //读..........
    }

    {
        tg_rwlock::write_guard(g_rwlock);

        //写..........
    }
}

tg_rwlock.h 

#ifndef TG_RWLOCK_H
#define TG_RWLOCK_H

#include <mutex>
#include <condition_variable>
#include <atomic>
#include <memory>

class tg_rwguard;

class tg_rwlock
{
    friend tg_rwguard;
private:
    std::atomic<uint32_t> _read_cnt{ 0 };
    std::atomic<uint32_t> _write_cnt{ 0 };

    std::atomic<bool> _is_writing{false};

    std::mutex _mutex;
    std::condition_variable _read_cond;
    std::condition_variable _write_cond;

public:
    tg_rwlock() = default;
    ~tg_rwlock() = default;

    static std::shared_ptr<tg_rwguard> read_guard(tg_rwlock& rwlock)
    {
        return std::make_shared<tg_rwguard>(rwlock, true);
    }

    static std::shared_ptr<tg_rwguard> write_guard(tg_rwlock& rwlock)
    {
        return std::make_shared<tg_rwguard>(rwlock, false);
    }

private:
    void rlock()
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _read_cond.wait(lock, [=] {return _write_cnt == 0; });
        ++_read_cnt;
    }

    void wlock()
    {
        std::unique_lock<std::mutex> lock(_mutex);
        ++_write_cnt;
        _write_cond.wait(lock, [=] {return _read_cnt == 0 && !_is_writing; });
        _is_writing = true;
    }

    void runlock()
    {
        std::unique_lock<std::mutex> lock(_mutex);
        if (_read_cnt>0 && --_read_cnt == 0 && _write_cnt > 0)
        {
            _write_cond.notify_one();
        }
    }

    void wunlock()
    {
        std::unique_lock<std::mutex> lock(_mutex);
        if(_write_cnt < 1)
        {
            return;
        }

        if (--_write_cnt == 0)
        {
            _read_cond.notify_all();
        }
        else
        {
            _write_cond.notify_one();
            _is_writing = false;
        }
    }
};

class tg_rwguard
{
public:
    explicit tg_rwguard(tg_rwlock &rwlock, bool is_read) : _rwlock(rwlock),_is_read(is_read)
    {
        if(_is_read)
        {
            _rwlock.rlock();
        }
        else
        {
            _rwlock.wlock();
        }
    }
    ~tg_rwguard()
    {
        if(_is_read)
        {
            _rwlock.runlock();
        }
        else
        {
            _rwlock.wunlock();
        }
    }
private:
    tg_rwguard() = delete;
    tg_rwguard(const tg_rwguard&) = delete;
    tg_rwguard& operator=(const tg_rwguard&) = delete;
private:
    tg_rwlock& _rwlock;
    bool _is_read;
};


#endif // TG_RWLOCK_H

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值