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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:25:21+00:00 2026-05-11T13:25:21+00:00

I have a generic dictionary in a multithreaded application; to implement a lock i

  • 0

I have a generic dictionary in a multithreaded application; to implement a lock i created a property.

static object myLock=new object(); Dictionary<key,SomeRef> dict=new Dictionary<key,SomeRef>();  public Dictionary<key,SomeRef> MyDict{       get{         lock(myLock){         return dict;         }      }  } 

Now if i write CODE#1

 MyDict.TryGetValue 

or CODE#2

var result=MyDict.Values; foreach(var item in result){ //read value into some other variable } 

so while i m runnig code 1 or 2 and at the same time if some other thread tries to do some write operation on the dictionary like ..clear dict or add new item. then, will this solution be thread safe (using a property). if not ..then is there any other ways to do this.

When i say write operation it can be take a reference of the dict through property chek key exoist or not if not create key and assign value. (thus me not using the setter in the property)

  • 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-11T13:25:22+00:00Added an answer on May 11, 2026 at 1:25 pm

    No, this will not be threadsafe.

    The lock will only lock around getting the reference to your internal (dict) instance of the dictionary. It will not lock when the user tries to add to the dictionary, or read from the dictionary.

    If you need to provide threadsafe access, I would recommend keeping the dictionary private, and make your own methods for getting/setting/adding values to/from the dictionary. This way, you can put the locks in place to protect at the granularity you need.


    This will look something like this:

    public bool TryGetValue(key thekey, out SomeRef result) {     lock(myLock) { return this.dict.TryGetValue(thekey, out result); } } public void Add(key thekey, SomeRef value) {     lock(myLock) { this.dict.Add(thekey, value) } } // etc for each method you need to implement... 

    The idea here is that your clients use your class directly, and your class handles the synchronization. If you expect them to iterate over the values (such as your foreach statement), you can decide whether to copy the values into a List and return that, or provide an enumerator directly (IEnumerator<SomeRef> GetValues()), etc.

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

Sidebar

Ask A Question

Stats

  • Questions 101k
  • Answers 101k
  • 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 Change HorizontalAlignment to Stretch or get rid of it entirely. May 11, 2026 at 8:05 pm
  • Editorial Team
    Editorial Team added an answer I have to say you could have provided a bit… May 11, 2026 at 8:05 pm
  • Editorial Team
    Editorial Team added an answer $str = '152.15 x 12.34 x 11mm'; preg_match_all('!\d+(?:\.\d+)?!', $str, $matches);… May 11, 2026 at 8:05 pm

Related Questions

I have used the select keyword and extension method to return an IEnumerable<T> with
I have a class that inherits a generic dictionary and an inteface public class
I'm using C# in Visual Studio 2008 with .NET 3.5. I have a generic
I am using LINQ to query a generic dictionary and then use the result

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.