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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:52:43+00:00 2026-05-24T21:52:43+00:00

When a thread tries to enter a critical section and obtain a lock, what

  • 0

When a thread tries to enter a critical section and obtain a lock, what is it actually doing?

I’m asking this because I usually create an object (of type object) which will serve for locking purposes only.
Consider the following: I want to write a method which accepts a collection, and an object which will serve as the locking object so the whole collection manipulation inside that method will be declared inside the critical section which will be locked by that given object.

Should I pass that locking object using “ref” or is passing a reference copy of that object is enough? In other words – since the lock statement is used with reference types only, does the mechanism check the value of the referenced object, or does it check the pointer‘s value? because obviously, when passing an object without “ref”, I actually get a copy of the reference, and not the reference itself.

  • 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-24T21:52:44+00:00Added an answer on May 24, 2026 at 9:52 pm

    Here’s a typical pattern that you can follow for locking. Basically, you can create a locking object that is used to lock access to your critical section (which, as @Hans said, is not protecting the object that you’re working on — it just handles the lock).

    class ThreadSafe
    {
      static readonly object _locker = new object();
      static int _val1, _val2;
    
      static void Go()
      {
        lock (_locker)
        {
          if (_val2 != 0) Console.WriteLine (_val1 / _val2);
          _val2 = 0;
        }
      }
    }
    

    This example was from Joseph Albahari’s online book on threading. It provides an excellent overview of what’s going on when you create a lock statement and some tips/tricks on how to best optimize for it. Definitely highly recommended reading.

    Per Albahari, again, the lock statement translates in .NET 4 as:

    bool lockTaken = false;
    try
    {
      Monitor.Enter (_locker, ref lockTaken);
      // Do your stuff...
    }
    finally { if (lockTaken) Monitor.Exit (_locker); }
    

    It’s actually safer than a straight Monitor.Enter and then calling Monitor.Exit in your finally, which is why it was added in .NET 4.

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

Sidebar

Related Questions

Using the lock statement , one can ensure that one thread does not enter
This reddit thread says 6 trits ~ 9.5 bits . How is 6 trits
I tried the solution from this thread , but I must be missing something
I was browsing this thread , which has good recommendation but a bit too
If the main thread quits before child finishes its work, and child tries to
Server creates new thread in a threadpool. This thread reads some stuff into buffer
Suppose I have a function that tries to protect a global counter using this
__thread Foo foo; How is foo actually resolved? Does the compiler silently replace every
Can Thread.getContextClassLoader() be null ? The javadoc is not really clear. Should a library
Can Thread.Abort interrupt a thread that is sleeping (using, say, Thread.Sleep(TimeSpan.FromDays(40)) ? Or will

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.