Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

My Reader-Writer Lock implementation. Need critics.

62 views
Skip to first unread message

Anton Sharov

unread,
Jul 24, 2012, 6:48:52 AM7/24/12
to
Hello everybody.

I decided to impelement reader-writer lock algorithm using TATAS approach.

I've impelemented this algorithm on .net platform:

//Shared variables:

private volatile bool _hasWriters;
private int _lock;
private long _readerCounter;


//Writer logic:

public void AcuqireWriterLock()
{
_hasWriters = true;
while(Interlocked.Exchange(ref _lock, 1) != 0)
{
while (_lock != 0)
{}
}

_hasWriters = true;
while (Interlocked.Read(ref _readerCounter) != 0)
{}
}

public void ExitWriterLock()
{
_hasWriters = false;
Interlocked.Exchange(ref _lock, 0);
}

//Reader logic:

public void AcuqireReaderLock()
{
X:
Interlocked.Increment(ref _readerCounter);
if (_hasWriters)
{
Interlocked.Decrement(ref _readerCounter);
while (_hasWriters)
{}
goto X;
}
}

public void ExitReaderLock()
{
Interlocked.Decrement(ref _readerCounter);
}


Do not judge strictly. I know that it is not production ready implementation,
I'm just trying to better understand concurrency algorithms and this is my first attempt to design one. I would like to hear critics about fairness, deadlock freedom, effectiveness and etc.

Thanks in advance.

Ronald Landheer-Cieslak

unread,
Jul 30, 2012, 1:55:44 PM7/30/12
to
_hasWriters and _lock seem to have different versions of the same
information - why have both of them?

You don't (seem to) do any ordering of readers or writers, so whichever
comes out of the while loops first wins and can starve any of the others --
you want comments on fairness, but you don't seem to make any provisions
for fairness?

rlc

--
Software analyst & developer -- http://rlc.vlinder.ca

Anton Sharov

unread,
Aug 22, 2012, 3:17:07 AM8/22/12
to
Hello, Ronald!

Thank you very, very much for your response!
I'm so sorry for the month delay but it is smth strange with google groups mail
delivery system. After they have updated UI it has stopped to send me email notifications...

Nevertheless, I was doing task from PLP book and there were conditions to use TATAS approach and condition that if there are always readers then writers mustn't starve forever.

So _haswriter used to protect writers from starvation from infinitely many readers and _lock used to guarantee mutual exclusion between writers.

Thank you once again for your response and critical view.
0 new messages