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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:34:32+00:00 2026-05-22T03:34:32+00:00

I see that for using objects which are not thread safe we wrap the

  • 0

I see that for using objects which are not thread safe we wrap the code with a lock like this:

private static readonly Object obj = new Object();

lock (obj)
{
    // thread unsafe code
}

So, what happens when multiple threads access the same code (let’s assume that it is running in a ASP.NET web application). Are they queued? If so how long will they wait?

What is the performance impact because of using locks?

  • 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-22T03:34:33+00:00Added an answer on May 22, 2026 at 3:34 am

    The lock statement is translated by C# 3.0 to the following:

    var temp = obj;
    
    Monitor.Enter(temp);
    
    try
    {
        // body
    }
    finally
    {
        Monitor.Exit(temp);
    }
    

    In C# 4.0 this has changed and it is now generated as follows:

    bool lockWasTaken = false;
    var temp = obj;
    try
    {
        Monitor.Enter(temp, ref lockWasTaken);
        // body
    }
    finally
    {
        if (lockWasTaken)
        {
            Monitor.Exit(temp); 
        }
    }
    

    You can find more info about what Monitor.Enter does here. To quote MSDN:

    Use Enter to acquire the Monitor on
    the object passed as the parameter. If
    another thread has executed an Enter
    on the object but has not yet executed
    the corresponding Exit, the current
    thread will block until the other
    thread releases the object. It is
    legal for the same thread to invoke
    Enter more than once without it
    blocking; however, an equal number of
    Exit calls must be invoked before
    other threads waiting on the object
    will unblock.

    The Monitor.Enter method will wait infinitely; it will not time out.

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

Sidebar

Related Questions

This is something that I solved using reflection, but would like to see how
I have some code that is using SyncEnumerator. As you can see here ,
EDIT: See this in action here: http://jsbin.com/emobi/5 -- and that's using mouseenter/mouseleave. I have
While googling, I see that using java.io.File#length() can be slow. FileChannel has a size()
I see that you can enable/disable using the EnableWindow method, but how do I
Using IE8's developer mode, I see that select lists are implemented using partial postbacks.
I've been using C/C++ and Python, but I now I see that a lot
What is the advantages/disadvantages of using inline functions in C++? I see that it
I'm using the command line compiler for builds. One problem I see is that
Currently we're using javascript new RegExp('#[^,#=!\s][^,#=!\s]*') (see [1]) and it mostly works, except that

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.