Using a Readers/Writer Lock Wrapper

As with the mutexes described before, the readers/writer lock is normally used in conjunction with a locker class, IlsRWLocker, that locks the resource in its constructor and unlocks the resource in its destructor.

Here is an example:

class connection

{

public:

...

int getConnectionCount();

void setConnectionCount(int);

private:

...

int _connectionCount;

IlsRWLock _protectConnCount;

};

 

int getConnectionCount()

{

IlsRWLocker locker(_protectConnCount, ILS_READ_LOCK);

return _connectionCount;

}

 

void setConnectionCount(int count)

{

IlsRWLocker locker(_protectConnCount, ILS_WRITE_LOCK);

_connectionCount = count;

}

As for the condition variable and the barrier class, the readers/writer lock can have a private mutex. In this case, the mutex must be locked before the member functions are called and unlocked once the member functions have been called.