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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:54:31+00:00 2026-05-27T00:54:31+00:00

I recently ran across this code in one of the projects I’m working on,

  • 0

I recently ran across this code in one of the projects I’m working on, and I’m not sure how it is supposed to behave.

private static object _syncRoot = 1;

public void DoSomething()
{
    lock (_syncRoot)
    {
        // do stuff...
    }
}

I know that it’s customary to use new object() for your lock. How will this lock behave being locked on a boxed integer? What happens if you add another lock with the same value?

private static object _anotherLock = 1;

public void DoSomethingElse()
{
    lock (_anotherLock)
    {
        // do stuff...
    }
}
  • 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-27T00:54:31+00:00Added an answer on May 27, 2026 at 12:54 am

    Though not directly related to boxing, this could break if a string like “1” were used instead of an int. Two strings with the same constant value can have the same reference and therefore locking on the ‘separate’ strings would use the same sync block. In general, I would recommend new object() [a known good pattern] over a constant like this.

    In fact, I’m not sure if boxing to unique instances is actually in the spec. I could certainly see creating a ‘boxing pool’ for common constants (Like 0 or 1) as a potential CLR improvement. If that were implemented, this usage would fail (in a quite nasty and unpredictable way, most likely.)

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

Sidebar

Related Questions

I recently ran across some code which the person did the first one. Would
I recently ran into a piece of code very much like this one: var
I ran across this code recently which is part of a template class: operator
I recently ran into this problem: Rails 3 link_to (:method => :delete) not working
I ran across this case of UnboundLocalError recently, which seems strange: import pprint def
I recently ran across some 3rd party C# code which does the following: public
I ran across some code recently at work (recreated to be similar to what
I recently ran across a set of code which instantiated local maps as following:
I recently ran across a routine that looks something like this: procedure TMyForm.DoSomething(list: TList<TMyObject>;
I recently ran across this great article by Chad Parry entitled DIY-DI or Do-It-Yourself

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.