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


Groups > comp.programming.threads > #986

Re: My Reader-Writer Lock implementation. Need critics.

From Ronald Landheer-Cieslak <rlc@vlinder.ca>
Newsgroups comp.programming.threads
Subject Re: My Reader-Writer Lock implementation. Need critics.
Date 2012-07-30 17:55 +0000
Organization albasani.net
Message-ID <1989186785365363299.237068rlc-vlinder.ca@reader.albasani.net> (permalink)
References <691bffec-a5b7-4d43-9de4-7fbb755d4048@googlegroups.com>

Show all headers | View raw


Anton Sharov <ocaml@mail.ru> wrote:
> 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.
_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

Back to comp.programming.threads | Previous | NextPrevious in thread | Next 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