Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #974

My Reader-Writer Lock implementation. Need critics.

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Anton Sharov <ocaml@mail.ru>
Newsgroups comp.programming.threads
Subject My Reader-Writer Lock implementation. Need critics.
Date Tue, 24 Jul 2012 03:48:52 -0700 (PDT)
Organization http://groups.google.com
Lines 60
Message-ID <691bffec-a5b7-4d43-9de4-7fbb755d4048@googlegroups.com> (permalink)
NNTP-Posting-Host 89.175.180.246
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1343126933 14846 127.0.0.1 (24 Jul 2012 10:48:53 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Tue, 24 Jul 2012 10:48:53 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=89.175.180.246; posting-account=oBSkWAoAAACHe4mEO4sY1LNbbbZIntwp
User-Agent G2/1.0
Xref csiph.com comp.programming.threads:974

Show key headers only | View raw


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.

Back to comp.programming.threads | Previous | NextNext in thread | Find similar


Thread

My Reader-Writer Lock implementation. Need critics. Anton Sharov <ocaml@mail.ru> - 2012-07-24 03:48 -0700
  Re: My Reader-Writer Lock implementation. Need critics. Ronald Landheer-Cieslak <rlc@vlinder.ca> - 2012-07-30 17:55 +0000
    Re: My Reader-Writer Lock implementation. Need critics. Anton Sharov <ocaml@mail.ru> - 2012-08-22 00:17 -0700

csiph-web