Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 141767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:49:46+00:00 2026-05-11T07:49:46+00:00

I have an adopted implementation of a simple (no upgrades or timeouts) ReaderWriterLock for

  • 0

I have an adopted implementation of a simple (no upgrades or timeouts) ReaderWriterLock for Silverlight, I was wondering anyone with the right expertise can validate if it is good or bad by design. To me it looks pretty alright, it works as advertised, but I have limited experience with multi-threading code as such.

public sealed class ReaderWriterLock {     private readonly object syncRoot = new object();    // Internal lock.     private int i = 0;                                  // 0 or greater means readers can pass; -1 is active writer.     private int readWaiters = 0;                        // Readers waiting for writer to exit.     private int writeWaiters = 0;                       // Writers waiting for writer lock.     private ConditionVariable conditionVar;             // Condition variable.      public ReaderWriterLock()     {         conditionVar = new ConditionVariable(syncRoot);     }      /// <summary>     /// Gets a value indicating if a reader lock is held.     /// </summary>     public bool IsReaderLockHeld     {         get         {             lock ( syncRoot )             {                 if ( i > 0 )                     return true;                 return false;             }         }     }      /// <summary>     /// Gets a value indicating if the writer lock is held.     /// </summary>     public bool IsWriterLockHeld     {         get         {             lock ( syncRoot )             {                 if ( i < 0 )                     return true;                 return false;             }         }     }      /// <summary>     /// Aquires the writer lock.     /// </summary>     public void AcquireWriterLock()     {         lock ( syncRoot )         {             writeWaiters++;             while ( i != 0 )                 conditionVar.Wait();      // Wait until existing writer frees the lock.             writeWaiters--;             i = -1;             // Thread has writer lock.         }     }      /// <summary>     /// Aquires a reader lock.     /// </summary>     public void AcquireReaderLock()     {         lock ( syncRoot )         {             readWaiters++;             // Defer to a writer (one time only) if one is waiting to prevent writer starvation.             if ( writeWaiters > 0 )             {                 conditionVar.Pulse();                 Monitor.Wait(syncRoot);             }             while ( i < 0 )                 Monitor.Wait(syncRoot);             readWaiters--;             i++;         }     }      /// <summary>     /// Releases the writer lock.     /// </summary>     public void ReleaseWriterLock()     {         bool doPulse = false;         lock ( syncRoot )         {             i = 0;             // Decide if we pulse a writer or readers.             if ( readWaiters > 0 )             {                 Monitor.PulseAll(syncRoot); // If multiple readers waiting, pulse them all.             }             else             {                 doPulse = true;             }         }         if ( doPulse )             conditionVar.Pulse();                     // Pulse one writer if one waiting.     }      /// <summary>     /// Releases a reader lock.     /// </summary>     public void ReleaseReaderLock()     {         bool doPulse = false;         lock ( syncRoot )         {             i--;             if ( i == 0 )                 doPulse = true;         }         if ( doPulse )             conditionVar.Pulse();                     // Pulse one writer if one waiting.     }      /// <summary>     /// Condition Variable (CV) class.     /// </summary>     public class ConditionVariable     {         private readonly object syncLock = new object(); // Internal lock.         private readonly object m;                       // The lock associated with this CV.          public ConditionVariable(object m)         {             lock (syncLock)             {                 this.m = m;             }         }          public void Wait()         {             bool enter = false;             try             {                 lock (syncLock)                 {                     Monitor.Exit(m);                     enter = true;                     Monitor.Wait(syncLock);                 }             }             finally             {                 if (enter)                     Monitor.Enter(m);             }         }          public void Pulse()         {             lock (syncLock)             {                 Monitor.Pulse(syncLock);             }         }          public void PulseAll()         {             lock (syncLock)             {                 Monitor.PulseAll(syncLock);             }         }      }  }  

If it is good, it might be helpful to others too as Silverlight currently lacks a reader-writer type of lock. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:49:47+00:00Added an answer on May 11, 2026 at 7:49 am

    I go in depth on explaining Vance Morrison’s ReaderWriterLock (which became ReaderWriterLockSlim in .NET 3.5) on my blog (down to the x86 level). This might be helpful in your design, especially understanding how things really work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have wholly adopted the MVVM pattern for our silverlight app. However, some of
I have adopted a technique where I pass existing objects to forms so that
I need to have a command handler for a ToggleButton that can take multiple
I work for a large company that has adopted sharepoint. I have been tasked
I have recently decided to adopted Codeigniter as a PHP framework. I would like
Our team at work has enthusiastically adopted a rebase workflow, but we might have
FYI I have adopted the mediator pattern for my GUI in Swing on Java.
Following suggestions on this site, I have adopted SimpleXML from org.simpleframework.xml. I use this
A few years back, I have adopted the following pattern for all except the
Ok I have adopted a CodeIgniter Setup, My route.php in config looks like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.