C++ 读写锁设计

C++ 读写锁设计,第1张

C++ 读写锁设计
C++ 读写锁设计
本文档为自实现的读写锁,可直接使用
本方案没有用到一切外部库,仅使用到C++11

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

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


#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 m_lkRw;

	//写入中
	std::atomic 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
#include "stdafx.h"

#include 
#include 
#include 
#include 

#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 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::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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5698714.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存