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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:27:59+00:00 2026-06-10T13:27:59+00:00

I am having trouble understanding the way to use the lock() statement in my

  • 0

I am having trouble understanding the way to use the lock() statement in my code. I have a couple static collections like so:

private static Dictionary<string, User>() Users = new Dictionary<string, User>();

I constantly add, remove, update, and read from this collection. I realize that when I add, remove, or update I should be locking the Users, but when I read from the collection do I have to lock it? What is the correct way to do something like search for a key and return the User in the Dictionary? I was thinking creating a new Dictionary instance and then copy Users to that and then read from it, or can I just read directly from it?

  • 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-06-10T13:28:01+00:00Added an answer on June 10, 2026 at 1:28 pm

    The best option here would likely to be removing the locks, and using ConcurrentDictionary<string, User> instead of a Dictionary<string, User>.

    Otherwise, you will need to synchronize your reads as well. You can read from multiple threads, but if a writer will be writing to the dictionary while readers are reading, you need synchronization. ReaderWriterLock (or ReaderWriterLockSlim) work well in this scenario, as they can allow multiple readers, but only a single writer. A simple lock will also work, but will block more often than required.

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

Sidebar

Related Questions

I am having trouble understanding the foursquare Terms of Use concerning the Venues API.
I'm having trouble understanding why I would use a context.xml file to declare a
I am having trouble understanding how layering is performed within Xcode. I have a
I'm having trouble understanding the following bit of code that I was hoping would
I'm having some trouble understanding how to use coroutines properly with luabind. There's a
I am having a little trouble understanding migrations in Ruby on Rails. I have
I'm having trouble understanding the for/in statement in JavaScript. The book which I'm using
I am having some trouble understanding how to use Unix's fork() . I am
I'm having trouble understanding some basic memory management principles in C++. This code is
Having trouble understanding how to filter an images table by tag information in a

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.