现在的位置: 首页 > 综合 > 正文

一个简单的读写锁的实现(WINDOWS平台)

2012年07月30日 ⁄ 综合 ⁄ 共 2916字 ⁄ 字号 评论关闭

#include
<windows.h>
class CReadWriteLock
{
private:
    LONG
mdwReaders;
   
CRITICAL_SECTION mEnterLock;
public:
   
CReadWriteLock(void);
   
~CReadWriteLock(void);
    bool
ReadLock();
    bool
ReadUnlock();
    bool
WriteLock();
    bool
WriteUnlock();
};

/***************************************************

CReadWriteLock::CReadWriteLock(void)
构造函数
*****************************************************/
inline CReadWriteLock::CReadWriteLock(void)
{
   
InitializeCriticalSection(&mEnterLock);
    mdwReaders =
0;
    #ifdef
_FAN_DEBUG__
       
std::cout<<"Create ReadWriteLock"<<endl;
    #endif
//_FAN_DEBUG__
}
/***************************************************
CReadWriteLock::~CReadWriteLock(void)
析构函数
*****************************************************/
inline CReadWriteLock::~CReadWriteLock(void)
{
   
DeleteCriticalSection(&mEnterLock);
    #ifdef
_FAN_DEBUG__
       
std::cout<<"Delete ReadWriteLock"<<endl;
    #endif
//_FAN_DEBUG__
}
/***************************************************
CReadWriteLock::ReadLock()
读加锁
返回植:true       
锁定成功
       
false      
其它情况
*****************************************************/
inline bool CReadWriteLock::ReadLock()
{
   
EnterCriticalSection(&mEnterLock);
   
InterlockedIncrement(&mdwReaders);
   
LeaveCriticalSection(&mEnterLock);
    #ifdef
_FAN_DEBUG__
       
std::cout<<"Reader lock success."<<endl;
    #endif
//_FAN_DEBUG__
    return
true;
}
/***************************************************
CReadWriteLock::ReadUnlock()
读开锁
返回植:true       
锁定成功
       
false      
其它情况
*****************************************************/
inline bool CReadWriteLock::ReadUnlock()
{
   
InterlockedDecrement(&mdwReaders);
    #ifdef
_FAN_DEBUG__
       
std::cout<<"Reader unlock success."<<endl;
    #endif
//_FAN_DEBUG__
    return
true;
}
/***************************************************
CReadWriteLock::WriteLock()
写加锁
返回植:true       
锁定成功
       
false      
其它情况
*****************************************************/
inline bool CReadWriteLock::WriteLock()
{
    //HANDLE
mhThread = GetCurrentThread();
    //BOOL
mbSetPrioritySuccess = FALSE;
   
EnterCriticalSection(&mEnterLock);
   
//直到读的用户全部退出
    //if
(mdwReaders>0)
   
//{
   
//       
mbSetPrioritySuccess =
   
//       
SetThreadPriority(mhThread,THREAD_PRIORITY_LOWEST);
   
//}
   
while(mdwReaders>0)
    {
    #ifdef
_FAN_DEBUG__
       
std::cout<<"Writer lock test Reader
number."<<endl;
       
std::cout<<"Current Reader number is
"<<mdwReaders<<endl;
    #endif
//_FAN_DEBUG__
       
Sleep(0);
    }
    //if
(mbSetPrioritySuccess)
   
//{
   
// 
SetThreadPriority(mhThread,THREAD_PRIORITY_NORMAL);
   
//}
    #ifdef
_FAN_DEBUG__
       
std::cout<<"Writer lock success."<<endl;
    #endif
//_FAN_DEBUG__
    return
true;
}

/***************************************************

CReadWriteLock::WriteUnlock()
写解锁
返回植:true       
锁定成功
       
false      
其它情况
*****************************************************/
inline bool CReadWriteLock::WriteUnlock()
{
   
LeaveCriticalSection(&mEnterLock);
    #ifdef
_FAN_DEBUG__
       
std::cout<<"Writer unlock success."<<endl;
    #endif
//_FAN_DEBUG__
    return
true;
}

抱歉!评论已关闭.