Boost logo

Boost :

From: Alexander Terekhov (terekhov_at_[hidden])
Date: 2001-08-13 05:36:10


> > > 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)
> {
> }
> };
> };

thanks. note however, while using WaitForMultiple( mutex, event... )
you have precluded various race conditions but you are paying rather
high price for it:

a) you have to synchronize quite expensive set/reset event calls
(you do it with mutex locked making internal event synchronization
just a useless/boring overhead for you);

b) with WaitForMultiple( mutex, event... ) you have introduced
another monitor (it watches the states of multiple synch.
objects) which means that you've got even more internal locking
and redundant internal signaling/wakeups/checks/block-again and/or
internal transfer of synch. objects (mutex) ownership across thread
context switches..

Q) taking into account the issues above do you still think that
your solution is better than (or just "equal to") some CV(s)
based solution; e.g:

http://www.aw.com/cseng/titles/0-201-63392-2/code/rwlock.c

?

regards,
alexander.


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