Boost logo

Boost :

From: flameframe_at_[hidden]
Date: 2001-08-13 04:23:05


--- In boost_at_y..., "Alexander Terekhov" <terekhov_at_d...> wrote:
>
>
> > I have written some high level abstractions
> > like reader-writer lock using events without getting something
like
> > CVs in source code.
>
> could you please show your reader-writer lock using events ?
>
> regards,
> alexander.

Just a cut-edit-and-paste from simple redaction of a class:

// Comments :
// class Mutex - wraps Win32 mutex API
// class Event - wraps Win32 event API
// class Enter - wraps WaitForSingleObjectEx/WaitForMultipleObjectsEx
API
//Usage:
//class A : RWLock
//{
// void read_function() const
// {
// ReadEnter guard(this);
// ....
// }
// void write_function()
// {
// WriteEnter guard(this);
// ....
// }
//};
struct RWLock
{
           class ReadEnter; class WriteEnter;
    friend class ReadEnter; friend class WriteEnter;

    Mutex lock;
    Event noReaders;

    int numOfReaders;

    
    void oneReaderMore()
    {
        numOfReaders++;
        noReaders = false; //Reset manual reset-event
    }
    void oneReaderLess()
    {
        if( --numOfReaders == 0 ) // ( no more readers )
        {
            noReaders = true; //Set manual reset-event
        }
    }
    RWLock()
    {
        numOfReaders = 0 ;
        noReaders = true ;
    }

    class ReadEnter
    {
        RWLock* s;
        DWORD timeout;
    public:
        explicit ReadEnter( RWLock*s_
                          , DWORD timeout_=INFINITE)
            : s(s_),timeout(timeout_)
        {
            Enter e(s->lock,timeout);
            s->oneReaderMore();
        }
       ~ReadEnter()
        {
            Enter e(s->lock,timeout);
            s->oneReaderLess();
        }
    };
    class WriteEnter : public Enter
    {
    public:
        explicit WriteEnter( RWLock*s_
                           , DWORD timeout=INFINITE)
            : Enter(s->lock, s->noReaders
            , waitAll, timeout)
        {
        }
    };
};

Regards,
Vitalij.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk