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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:20:34+00:00 2026-05-28T01:20:34+00:00

I have been learning about locking on threads and I have not found an

  • 0

I have been learning about locking on threads and I have not found an explanation for why creating a typical System.Object, locking it and carrying out whatever actions are required during the lock provides the thread safety?

Example

object obj = new object()
lock (obj) {
  //code here
}

At first I thought that it was just being used as a place holder in examples and meant to be swapped out with the Type you are dealing with. But I find examples such as Dennis Phillips points out, doesn’t appear to be anything different than actually using an instance of Object.

So taking an example of needing to update a private dictionary, what does locking an instance of System.Object do to provide thread safety as opposed to actually locking the dictionary (I know locking the dictionary in this case could case synchronization issues)?
What if the dictionary was public?

//what if this was public?
private Dictionary<string, string> someDict = new Dictionary<string, string>();

var obj = new Object();
lock (obj) {
  //do something with the dictionary
}
  • 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-28T01:20:35+00:00Added an answer on May 28, 2026 at 1:20 am

    It used to be common practice to lock on the shared data itself:

    private Dictionary<string, string> someDict = new Dictionary<string, string>();
    
    lock (someDict ) 
    {
      //do something with the dictionary
    }
    

    But the (somewhat theoretical) objection is that other code, outside of your control, could also lock on someDict and then you might have a deadlock.

    So it is recommended to use a (very) private object, declared in 1-to-1 correspondence with the data, to use as a stand-in for the lock. As long as all code that accesses the dictionary locks on on obj the tread-safety is guaranteed.

    // the following 2 lines belong together!!
    private Dictionary<string, string> someDict = new Dictionary<string, string>();
    private object obj = new Object();
    
    
    // multiple code segments like this
    lock (obj) 
    {
      //do something with the dictionary
    }
    

    So the purpose of obj is to act as a proxy for the dictionary, and since its Type doesn’t matter we use the simplest type, System.Object.

    What if the dictionary was public?

    Then all bets are off, any code could access the Dictionary and code outside the containing class is not even able to lock on the guard object. And before you start looking for fixes, that simply is not an sustainable pattern. Use a ConcurrentDictionary or keep a normal one private.

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

Sidebar

Related Questions

Recently I have been learning about WMI and WQL. I found out the list
I have been learning about the basics of C# but haven't come across a
I am currently learning about basic networking in java. I have been playing around
I've been learning wpf for about a week now.. and i have a basic
I am just learning C# (have been fiddling with it for about 2 days
I have been learning about IE's rather ridiculous-looking requirements for shadows, gradients, etc., and
In my class we have been learning about different designs such as MVC and
I have been neglecting learning about caching for quite some time now, and although
I've been learning about merge and include lately, and I have a question I
I am learning about preconditions and when to use them. I have been told

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.