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

推荐订阅源

量子位
S
Securelist
MyScale Blog
MyScale Blog
Jina AI
Jina AI
罗磊的独立博客
The Cloudflare Blog
美团技术团队
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
雷峰网
雷峰网
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
博客园 - 聂微东
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
T
Tailwind CSS Blog
Attack and Defense Labs
Attack and Defense Labs
博客园_首页
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
D
Docker
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
V2EX - 技术
V2EX - 技术
N
News | PayPal Newsroom
Microsoft Security Blog
Microsoft Security Blog

博客园 - 吾非无心

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)  评论()    收藏  举报