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

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
博客园 - 【当耐特】
小众软件
小众软件
博客园 - Franky
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
雷峰网
雷峰网
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Last Week in AI
Last Week in AI
博客园_首页
月光博客
月光博客
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
云风的 BLOG
云风的 BLOG
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Docker
The Last Watchdog
The Last Watchdog
有赞技术团队
有赞技术团队
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
DataBreaches.Net
S
Security @ Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
Y
Y Combinator Blog
O
OpenAI News
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
K
Kaspersky official blog
Cloudbric
Cloudbric

博客园 - 吾非无心

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