您当前的位置: 主页网站优化软件知识

在Windows实现C++锁的读写

发布于:2014-03-17 18:44:51  作者:兄弟网络   点击:
// 一个Windows下C++读写锁的代码,网站优化,实现共享读,独占写

class ReadWriteLock
{
public:
    int    m_currentLevel;
    int    m_readCount; 
    HANDLE m_unlockEvent;
    HANDLE m_accessMutex;
    CRITICAL_SECTION m_csStateChange;
public:
    ReadWriteLock()
    {
        m_currentLevel = LOCK_LEVEL_NONE;
        m_readCount    = 0;
        m_unlockEvent  = ::CreateEvent( NULL, TRUE, FALSE, NULL );
        m_accessMutex  = ::CreateMutex( NULL, FALSE, NULL );
        ::InitializeCriticalSection( &m_csStateChange );
    }
    ~ReadWriteLock()
    {
        ::DeleteCriticalSection( &m_csStateChange );
        if ( m_accessMutex ) ::CloseHandle( m_accessMutex );
        if ( m_unlockEvent ) ::CloseHandle( m_unlockEvent );
    }

    bool lock( int level, int timeout = INFINITE)
    {
        bool  bresult    = true;
        DWORD waitResult = 0;
      
        waitResult = ::WaitForSingleObject( m_accessMutex, timeout );
        if ( waitResult != WAIT_OBJECT_0 )  return false;

        if ( level == LOCK_LEVEL_READ && m_currentLevel != LOCK_LEVEL_WRITE )
        {
            ::EnterCriticalSection( &m_csStateChange );
            m_currentLevel = level;
            m_readCount += 1;
            ::ResetEvent( m_unlockEvent );
            ::LeaveCriticalSection( &m_csStateChange );
        }
        else if ( level == LOCK_LEVEL_READ &&
                  m_currentLevel == LOCK_LEVEL_WRITE )
        {
            waitResult = ::WaitForSingleObject( m_unlockEvent, timeout );
            if ( waitResult == WAIT_OBJECT_0 )
            {
                ::EnterCriticalSection( &m_csStateChange );
                m_currentLevel = level;
                m_readCount += 1;
                ::ResetEvent( m_unlockEvent );
                ::LeaveCriticalSection( &m_csStateChange );
            }
            else bresult = false;
        }
        else if ( level == LOCK_LEVEL_WRITE &&
                  m_currentLevel == LOCK_LEVEL_NONE )
        {
            ::EnterCriticalSection( &m_csStateChange );
            m_currentLevel = level;
            ::ResetEvent( m_unlockEvent );
            ::LeaveCriticalSection( &m_csStateChange );
        }
        else if ( level == LOCK_LEVEL_WRITE &&
                  m_currentLevel != LOCK_LEVEL_NONE )
        {
            waitResult = ::WaitForSingleObject( m_unlockEvent, timeout );
            if ( waitResult == WAIT_OBJECT_0 )
            {
                ::EnterCriticalSection( &m_csStateChange );
                m_currentLevel = level;
                ::ResetEvent( m_unlockEvent );
                ::LeaveCriticalSection( &m_csStateChange );
            }
            else bresult = false;
        }

        ::ReleaseMutex( m_accessMutex );
        return bresult;

    } // lock()
  
    bool unlock()
    {
        ::EnterCriticalSection( &m_csStateChange );
        if ( m_currentLevel == LOCK_LEVEL_READ )
        {
            m_readCount --;
            if ( m_readCount == 0 )
            {
                m_currentLevel = LOCK_LEVEL_NONE;
                ::SetEvent (m_unlockEvent);
            }
        }
        else if ( m_currentLevel == LOCK_LEVEL_WRITE )
        {
            m_currentLevel = LOCK_LEVEL_NONE;
            ::SetEvent ( m_unlockEvent );
        }
        ::LeaveCriticalSection( &m_csStateChange );

        return true;
    }
};  

网站SEO
本文关键词: Windows| 实现| 读写|

[相关阅读]

我们介绍

  兄弟网络科技工作室,专业从事日照百度推广,日照百度优化,日照网站建设,日照网络公司,日照网站制作,日照网站优化,日照软件制作。如果您感觉我们不错请分享↓给更多的人

收缩