惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
C
Check Point Blog
博客园_首页
B
Blog
D
Docker
U
Unit 42
量子位
I
InfoQ
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
GbyAI
GbyAI
L
LangChain Blog
云风的 BLOG
云风的 BLOG
博客园 - Franky
美团技术团队
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
Vercel News
Vercel News
Recent Announcements
Recent Announcements
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Google DeepMind News
Google DeepMind News

博客园 - 吾非无心

MSB4019 找不到导入的项目“C:\Users\...\AppData\Local\QtMsBuild\vs-debugtools\qt_import.props” - 吾非无心 - 博客园 类/结构最后一个成员为类(string)时可能会出现“堆损坏”(HEAP CORRUPTION DETECTED)错误 GCC在C语言中内嵌汇编 asm __volatile__ QT删除python中的单行注释 double转int QJson 存储(u)longlong有问题 QT QPixmap QImage内存泄漏 QMetaObject::connectSlotsByName: No matching signal for QT数据库连接管理类 QT多重继承带来的问题及解决办法,记录备查 QT6 源码编译Win32 x86 vc++高精度计时sleep stack smashing detected 莫名其妙的错误 strdump的问题 再加一个realloc的问题 codeblocks下libacl配置 centos8 安装、配置redis6 pyftpdlib中文乱码的解决之道 CentOS8让uwsgi开机自动启动django(无需登录,无需手动) libevent学习一
linux下简单封装读写锁
吾非无心 · 2020-10-03 · via 博客园 - 吾非无心
#ifndef CLEESRWLOCKER_INCLUDED
#define CLEESRWLOCKER_INCLUDED
#include <pthread.h>
#include <unistd.h>
#include <iostream>
using namespace std;
class CLeesRWLocker
{
public:
    CLeesRWLocker()
    {
        pthread_rwlock_init(&m_locker,NULL);
        m_bAbort=false;
        m_nReadCounter=0;
        m_nWriteCounter=0;

        cout<<"pthread_rwlock_init"<<endl;
    }
    ~CLeesRWLocker()
    {
        m_bAbort=true;
        while(m_nReadCounter||m_nWriteCounter)
            usleep(1);
        pthread_rwlock_destroy(&m_locker);

        cout<<"pthread_rwlock_init"<<endl;
    }
    bool ReadLock()
    {
        while(m_bAbort==false)
        {
            if(pthread_rwlock_tryrdlock(&m_locker)==0)
            {
                m_nReadCounter++;
                //cout<<"ReadLock OK"<<endl;
                return true;
            }
            usleep(0);
        }
        cout<<"ReadLock Failed"<<endl;
        return false;
    }

    bool WriteLock()
    {
        while(m_bAbort==false)
        {
            if(pthread_rwlock_trywrlock(&m_locker)==0)
            {
                m_nWriteCounter++;
                //cout<<"WriteLock OK"<<endl;
                return true;
            }
            usleep(0);
        }
        cout<<"WriteLock Failed"<<endl;
        return false;
    }

    bool TryReadLock(uint32_t nMillSeconds)
    {
        for(uint32_t i=0;i<nMillSeconds&&m_bAbort==false;i++)
        {
            if(pthread_rwlock_tryrdlock(&m_locker)==0)
            {
                m_nReadCounter++;
                cout<<"TryReadLock OK"<<endl;
                return true;
            }
            usleep(999);
        }
        cout<<"TryReadLock Failed"<<endl;
        return false;
    }

    bool TryWriteLock(uint32_t nMillSeconds)
    {
        for(uint32_t i=0;i<nMillSeconds&&m_bAbort==false;i++)
        {
            if(pthread_rwlock_trywrlock(&m_locker)==0)
            {
                m_nWriteCounter++;
                cout<<"TryWriteLock OK"<<endl;
                return true;
            }
            usleep(999);
        }
        cout<<"TryWriteLock Failed"<<endl;
        return false;
    }

    bool UnLock()
    {
        if(m_nWriteCounter>0)
            m_nWriteCounter=0;
        if(m_nReadCounter>0)
            m_nReadCounter--;

        //cout<<"UnLock m_nWriteCounter="<<m_nWriteCounter<<"   m_nReadCounter="<<m_nReadCounter<<endl;
        pthread_rwlock_unlock(&m_locker);
    }
    //终止所有读写锁
    void Abort()
    {
        m_bAbort=true;
    }
private:
    bool m_bAbort;
    pthread_rwlock_t    m_locker;
    uint32_t m_nReadCounter;
    uint32_t m_nWriteCounter;
};


#endif // CLEESRWLOCKER_INCLUDED

posted @ 2020-10-03 10:46  吾非无心  阅读(269)  评论()    收藏  举报