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 5994109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:46:18+00:00 2026-05-22T23:46:18+00:00

I went through the implementation of SyncHashtable in defined in .Net framework BCL. This

  • 0

I went through the implementation of SyncHashtable in defined in .Net framework BCL.

This class provides synchronized access to multiple readers and writers.

One of the methods is implemented as

public override Object this[Object key] {
            get {
                    return _table[key];
            }
            set {
                lock(_table.SyncRoot) {
                    _table[key] = value;
                }
            }
        }

In my opinion the get method should also have a lock on Syncroot before accessing the object.

Consider the scenario :

Thread 1 : Deleting keys from the Hashtable.

Thread 2 : Reading objects using keys.

If a context switch occurs in thread 2 while reading the object and if thread 1 deletes the object , then in such a case the read operation will fail or give inconsistent result.

Hence couldn’t we implement this method like this

public override Object this[Object key] {
            get {
                 lock(_table.SyncRoot) 
                  {   
                   return _table[key];
                  }
            }
            set {
                lock(_table.SyncRoot) {
                    _table[key] = value;
                }
            }
        }

Thanks
Vivek

  • 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. Editorial Team
    Editorial Team
    2026-05-22T23:46:18+00:00Added an answer on May 22, 2026 at 11:46 pm

    Locking the Hashtable for reading is not necessary because that is already thread safe under these circumstances.

    The documentation for Hashtable states:

    Hashtable is thread safe for use by multiple reader threads and a single writing thread.

    By locking the write access, there is in effect only a single writer, and it is therefore unnecessary to lock reads.

    Note that while this is true for Hashtable, it is not the case for Dictionary<TKey, TValue>.

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

Sidebar

Related Questions

I just went through some MVC tutorials after checking this site out for a
I went through local variables and class variables concept. But I had stuck at
I went through this tutorial Your First iPhone Application and it worked great. My
I went through the suggested related topics before asking this question but none of
I went through many threads and couldn't figure it out. Sorry if this is
I went through this tutorial (http://www.adobe.com/devnet/facebook/articles/flex_fbgraph_pt4.html) and got everything working, only to find out
I went through the process of setting this up once, but now I need
I went through a lot of answers for this question.But it's all about single
I went through this question from SO How to use <h:selectBooleanCheckbox> in <h:dataTable> to
I went through this similar question at https://stackoverflow.com/a/7410228/880040 and the 6th thing to check,

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.