大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
C++ 读写锁设计
C++ 读写锁设计
本文档为自实现的读写锁,可直接使用
本文档为自实现的读写锁,可直接使用
本方案没有用到一切外部库,仅使用到C++11
本方案没有用到一切外部库,仅使用到C++11
文章目录
1.本方案是允许读数据同时进行,但与写数据是互斥的存在,只有读数据操作所有完成后释放锁才允许写。反之亦成立。
2.本方案存在自旋式读写锁,减轻使用者的释放锁的烦恼,当自旋读写锁超出作用域则会自动释放锁。
/*
*FileName: RWLock.h
*CreateTime: 2021年11月22日
*Author: yjh
*
*Abbreviation:
*abbr1: RLock - 读锁
*abbr2: AutoRLock - 回旋读锁
*abbr3: WLock - 写锁
*abbr4: AutoWLock - 回旋写锁
*abbr5: lk - lock - 锁
*
*Description:
*功能1: 实现读写锁 - 读比较多的情况下节省资源与时间
*功能2:
*功能3:
*
*/
#pragma once
class CRWLock
{
friend class CRLock;
//friend class CAutoRLock;
friend class CWLock;
//friend class CAutoWLock;
public:
CRWLock();
~CRWLock();
private:
void ReadLock();
void ReadUnLock();
void WriteLock();
void WriteUnLock();
private:
//核心 - 读写锁
std::atomic<uint32_t> m_lkRw;
//写入中
std::atomic<bool> m_bWriting;
//防止m_lk数据异常
std::mutex m_mt;
};
// ------------------------------ CRLock -----------------------------------
class CRLock
{
public:
CRLock(CRWLock& rwlk);
~CRLock();
public:
void Lock();
void Unlock();
private:
CRWLock* m_pRwlk;
};
// ------------------------------ CAutoRLock -----------------------------------
class CAutoRLock
{
public:
CAutoRLock(CRWLock& rwlk);
~CAutoRLock();
private:
CRLock m_rlk;
};
// ------------------------------ CWLock -----------------------------------
class CWLock
{
public:
CWLock(CRWLock& rwlk);
~CWLock();
public:
void Lock();
void Unlock();
private:
CRWLock* m_pRwlk;
};
// ------------------------------ CAutoWLock -----------------------------------
class CAutoWLock
{
public:
CAutoWLock(CRWLock& rwlk);
~CAutoWLock();
private:
CWLock m_wlk;
};
#include "stdafx.h"
#include <atomic>
#include <mutex>
#include <thread>
#include <chrono>
#include "RWLock.h"
// ------------------------------ CRWLock -----------------------------------
CRWLock::CRWLock() :m_bWriting(false)
{
}
CRWLock::~CRWLock()
{
}
void CRWLock::ReadLock()
{
m_mt.lock();
//若此时写锁以上锁 - 等待解锁
while (m_bWriting)
std::this_thread::sleep_for(std::chrono::milliseconds(2));
m_lkRw++;
m_mt.unlock();
}
void CRWLock::ReadUnLock()
{
m_lkRw--;
}
void CRWLock::WriteLock()
{
m_mt.lock();
while (m_lkRw)
std::this_thread::sleep_for(std::chrono::milliseconds(2));
m_lkRw++;
m_bWriting = true;
m_mt.unlock();
}
void CRWLock::WriteUnLock()
{
m_lkRw--;
m_bWriting = false;
}
// ------------------------------ CRLock -----------------------------------
CRLock::CRLock(CRWLock& rwlk)
{
m_pRwlk = &rwlk;
}
CRLock::~CRLock()
{
}
void CRLock::Lock()
{
m_pRwlk->ReadLock();
}
void CRLock::Unlock()
{
m_pRwlk->ReadUnLock();
}
// ------------------------------ CAutoRLock -----------------------------------
CAutoRLock::CAutoRLock(CRWLock& rwlk) :m_rlk(rwlk)
{
m_rlk.Lock();
}
CAutoRLock::~CAutoRLock()
{
m_rlk.Unlock();
}
// ------------------------------ CWLock -----------------------------------
CWLock::CWLock(CRWLock& rwlk)
{
m_pRwlk = &rwlk;
}
CWLock::~CWLock()
{
}
void CWLock::Lock()
{
m_pRwlk->WriteLock();
}
void CWLock::Unlock()
{
m_pRwlk->WriteUnLock();
}
// ------------------------------ CAutoWLock -----------------------------------
CAutoWLock::CAutoWLock(CRWLock& rwlk) :m_wlk(rwlk)
{
m_wlk.Lock();
}
CAutoWLock::~CAutoWLock()
{
m_wlk.Unlock();
}
CRWLock lk;
void Thread4Read_0()
{
CAutoRLock rlk(lk);
printf("Thread4Read_0 Running\n");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
printf("Thread4Read_0 End\n");
}
void Thread4Read_1()
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
CAutoRLock rlk(lk);
printf("Thread4Read_1 Running\n");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
printf("Thread4Read_1 End\n");
}
void Thread4Write()
{
std::this_thread::sleep_for(std::chrono::milliseconds(200));
CAutoWLock wlk(lk);
printf("Thread4Write Running\n");
std::this_thread::sleep_for(std::chrono::milliseconds(100));
printf("Thread4Write End\n");
}
void main()
{
/*
*
* RunThread是自定义的方法 这里就不实现了,自行使用std::thread启动线程吧
*
*/
RunThread(std::bind(Thread4Read_0,this));
RunThread(std::bind(Thread4Read_0,this));
RunThread(std::bind(Thread4Read_0,this));
RunThread(std::bind(Thread4Read_1,this));
RunThread(std::bind(Thread4Write,this));
getchar();
return;
}
Thread4Read_0 Running
Thread4Read_0 Running
Thread4Read_0 Running
|
| 500 milliseconds later
|
Thread4Read_1 Running
|
| 500 milliseconds later
|
Thread4Read_0 End
Thread4Read_0 End
Thread4Read_0 End
|
| 500 milliseconds later
|
Thread4Read_1 End
Thread4Write Running
|
| 100 milliseconds later
|
Thread4Write End
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/169990.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...