开发者

ReadWrite lock using Boost.Threads (how to convert this simple class)

I am porting some code from windows to Linux (Ubuntu 9.10). I have a simple class (please see below), which uses windows functions to implement simple mutex locking. I want to use Boost.Threads to reimplement this, but that library is new to me.

Can someone point out the changes I need to make to the class below, in order tomuse Boost.Threads instead of the WIN specific functions?

#ifndef __my_READWRITE_LOCK_Header__
#define __my_READWRITE_LOCK_Header__

#include <windows.h>

//Simple RW lock implementation is shown below. 

#define RW_READERS_MAX 10
#define RW_MAX_SEMAPHORE_COUNT 10
#define RW_MUTEX_NAME       L"mymutex"
#define RW_SEMAPHORE_NAME   L"mysemaphore"
class CThreadRwLock
{
public:
    CThreadRwLock()
    {
        InitializeCriticalSection(&m_cs);
        m_hSem = CreateSemaphore(0, RW_READERS_MAX, RW_READERS_MAX, 0);     
    }

    ~CThreadRwLock()
    {
        DeleteCriticalSection(&m_cs);
        CloseHandle(m_hSem);
    }

    void AcquireReaderLock()
    {
        EnterCriticalSection(&m_cs);
        WaitForSingleObject(m_hSem, INFINITE);
        LeaveCriticalSection(&m_cs);
    }

    void AcquireWriterLock()
    {
        EnterCriticalSection(&m_cs);
        for(int i = 0; i < RW_READERS_MAX; i++)
        {
            WaitForSingleObject(m_hSem, INFINITE);          
        }
        LeaveCriticalSection(&m_cs);
    }

    void ReleaseReaderLock()
    {
        ReleaseSemaphore(m_hSem, 1, 0);
    }


    void ReleaseWriterLock()
    {
        ReleaseSemaphore(m_hSem, R开发者_Python百科W_READERS_MAX, 0);
    }

private:
    CRITICAL_SECTION    m_cs;
    HANDLE              m_hSem;
};




class CProcessRwLock
{
public:
    CProcessRwLock()
    {
        m_h = CreateMutex(NULL, FALSE, RW_MUTEX_NAME);
        m_hSem = CreateSemaphore(NULL, RW_MAX_SEMAPHORE_COUNT, RW_MAX_SEMAPHORE_COUNT, RW_SEMAPHORE_NAME);
    }

    ~CProcessRwLock()
    {
        CloseHandle(m_h);
    }

    void AcquireReaderLock()
    {
        WaitForSingleObject(m_h, INFINITE);
        ReleaseMutex(m_h);
    }

    void AcquireWriterLock()
    {
        WaitForSingleObject(m_h, INFINITE);
        for(int i = 0; i < RW_READERS_MAX; i++)
        {
            WaitForSingleObject(m_hSem, INFINITE);          
        }
        ReleaseMutex(m_h);
    }

    void ReleaseReaderLock()
    {
        ReleaseSemaphore(m_hSem, 1, 0);
    }


    void ReleaseWriterLock()
    {
        ReleaseSemaphore(m_hSem, RW_READERS_MAX, 0);
    }

private:
    HANDLE              m_h, m_hSem;
};


class AutoThreadRwLock
{
public:
    AutoThreadRwLock(const bool readlock = true):m_readlock(readlock)
    {
        if (readlock)
            m_lock.AcquireReaderLock();
        else
            m_lock.AcquireWriterLock();
    }

    ~AutoThreadRwLock()
    {
        if (m_readlock)
            m_lock.ReleaseReaderLock();
        else
            m_lock.ReleaseWriterLock();
    }

private:
    AutoThreadRwLock(const AutoThreadRwLock&);
    AutoThreadRwLock& operator= (const AutoThreadRwLock& );

    CThreadRwLock m_lock ;
    bool m_readlock ;
};


class AutoProcessRwLock
{
public:
    AutoProcessRwLock(const bool readlock = true): m_readlock(readlock)
    {
        if (readlock)
            m_lock.AcquireReaderLock();
        else
            m_lock.AcquireWriterLock();
    }

    ~AutoProcessRwLock()
    {
        if (m_readlock)
            m_lock.ReleaseReaderLock();
        else
            m_lock.ReleaseWriterLock();
    }

private:
    AutoProcessRwLock(const AutoProcessRwLock&);
    AutoProcessRwLock& operator= (const AutoProcessRwLock&);

    CProcessRwLock m_lock ;
    bool m_readlock ;
};

#endif //__my_READWRITE_LOCK_Header__


I'm not going to re-write all your code for you. However you should look into boost's shared_mutex class.

Also, this question from StackOverflow shows how to use a boost::shared_mutex

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜