c++读写锁实现_设计模式PDF

2022-09-22 19:48:05 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

C 读写锁设计

本文档为自实现的读写锁,可直接使用

本方案没有用到一切外部库,仅使用到C 11

文章目录
  • **简析:**
    •    1.本方案是允许读数据同时进行,但与写数据是互斥的存在,只有读数据操作所有完成后释放锁才允许写。反之亦成立。
    •    2.本方案存在自旋式读写锁,减轻使用者的释放锁的烦恼,当自旋读写锁超出作用域则会自动释放锁。
  • **原理:**
    •    读写锁内部存在一个计数器,通过计数器辨别目前有多少同时正在使用读取数据,以及存在一个是否正在写入数据的标识。
  • **代码块**
    • **RWLock.h**
    • **RWLock.cpp**
  • **使用例子:**
    • **执行结果:**

  • 简析:
   1.本方案是允许读数据同时进行,但与写数据是互斥的存在,只有读数据操作所有完成后释放锁才允许写。反之亦成立。
   2.本方案存在自旋式读写锁,减轻使用者的释放锁的烦恼,当自旋读写锁超出作用域则会自动释放锁。
  • 原理:
  •    读写锁内部存在一个计数器,通过计数器辨别目前有多少同时正在使用读取数据,以及存在一个是否正在写入数据的标识。
  • 代码块
  • RWLock.h
代码语言:javascript复制
/*
*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;
};
  • RWLock.cpp
代码语言:javascript复制
#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();
}

  • 使用例子:
代码语言:javascript复制
CRWLock lk;

void Thread4Read_0()
{
    CAutoRLock rlk(lk);
    
    printf("Thread4Read_0 Runningn");
    
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    
    printf("Thread4Read_0 Endn");

}

void Thread4Read_1()
{
    std::this_thread::sleep_for(std::chrono::milliseconds(500));
    CAutoRLock rlk(lk);
     
    printf("Thread4Read_1 Runningn");
    
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    
    printf("Thread4Read_1 Endn");

}


void Thread4Write()
{
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    CAutoWLock wlk(lk);
     
    printf("Thread4Write Runningn");
    
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    
    printf("Thread4Write Endn");
}


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;
}
  • 执行结果:
代码语言:javascript复制
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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/169990.html原文链接:https://javaforall.cn

0 人点赞