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

  • Home
  • SEARCH
  • 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 94669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:32:16+00:00 2026-05-10T23:32:16+00:00

If I am using ReaderWriterLockSlim to acquire read/write locks, do I need to make

  • 0

If I am using ReaderWriterLockSlim to acquire read/write locks, do I need to make my variables volatile or use Interlocked.Increment?

For example, would the code in the Add method below work fine, or does it need enhancement?

public class AppendableList<T> { // semi-immutable; supports appending only     private T[] data = new T[16];     private ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim();     public int Count { get; private set; }     public T this[int index] {         get {             rwLock.EnterReadLock();             try { return data[index]; } finally { rwLock.ExitReadLock(); }         }     }     public void Add(T item) {         rwLock.EnterUpgradeableReadLock();         try {             if (Count == data.Length)                 reAllocateArray(); // upgrades to write lock             data[Count++] = item; // do I need to use Interlocked here?         } finally { rwLock.ExitUpgradeableReadLock(); }     } } 

EDIT: I’m trying to write a light-weight, fast and simple list that allows multiple threads to access its data concurrently (sort of producer-consumer buffer). I have edited the code above removing the simplifications I used before, so the issue should be clearer now. It seems to me that this code is thread-safe, but I am not sure whether all threads will see the updated value of Count right after exiting the upgradeable lock.

EDIT 2: The ‘Write’ lock here is used to indicate writing to the array reference, not the array elements. I’m assuming this is sufficient (since the data itself is immutable). I guess that I need to use Interlocked when incrementing Count. Is that true?

  • 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-10T23:32:17+00:00Added an answer on May 10, 2026 at 11:32 pm

    I would fully expect the write-lock to function as a memory barrier (within the write-lock, in particular), but I can’t prove it off-hand.

    Whether you need the complexity of ReaderWriterLockSlim depends on the context; Interlocked, volatile, lock or [MethodImpl] might each do the job far more simply. You mainly need ReaderWriterLock[Slim] if you have lots of readers and few writers.

    However, the get is not currently protected by the lock; you’ll need to us an explicit property implementation and take out a read lock yourself if you ever need multiple operations to be spanned by the write-lock (without readers seeing intermediate values).

    As an aside, the Count++ usage would probably be more familiar to people.

    You should also use try/finally to ensure you release the lock on exception.

    To avoid the issue of taking write then read locks, perhaps:

        private ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim();      private int count;     public int Count {         get {             rwLock.EnterReadLock();             int tmp = count;             rwLock.ExitReadLock();             return tmp;         }     }     public void Add(object x) {         rwLock.EnterWriteLock();         try {             // do some processing             count++;         } finally {             rwLock.ExitWriteLock();         }     } 

    Updated re your edit;

    That looks pretty solid. A List<T> would be my recommendation (over a T[] array), since it will do all the doubling etc internally, saving you lots of code. Since only one thread can update Count at a time, there is no need for Interlocked, and this property saves the need for a lock when reading Count, as long as you’re fine for callers to get the old Count while another thread is adding rows (rather than being blocked).

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

Sidebar

Ask A Question

Stats

  • Questions 100k
  • Answers 100k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer F# and IronPython/IronRuby are light years apart from a language… May 11, 2026 at 7:57 pm
  • Editorial Team
    Editorial Team added an answer A Sub-Report is basically just a report inside of another… May 11, 2026 at 7:57 pm
  • Editorial Team
    Editorial Team added an answer I think this is correct. Not tested, though. from a… May 11, 2026 at 7:57 pm

Related Questions

We have a website running .NET 2.0 and have started using the ASP.Net HttpRuntime.Cache
If I am using a tree structure of nodes similar to the code below,
If I am using EventWaitHandle (or AutoResetEvent , ManualResetEvent ) to synchronise between threads
I am using VB.NET. In Visual Studio, if I right-click a property name and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.