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


Groups > comp.programming.threads > #974

My Reader-Writer Lock implementation. Need critics.

From Anton Sharov <ocaml@mail.ru>
Newsgroups comp.programming.threads
Subject My Reader-Writer Lock implementation. Need critics.
Date 2012-07-24 03:48 -0700
Organization http://groups.google.com
Message-ID <691bffec-a5b7-4d43-9de4-7fbb755d4048@googlegroups.com> (permalink)

Show all headers | 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