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

The Archive Base Latest Questions

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

This is from MSDN: The lock keyword ensures that one thread does not enter

  • 0

This is from MSDN:
The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section.

Does a critical section have to be same as the critical section?

Or does it mean:
The lock keyword ensures that one thread does not enter any critical section guarded by an object of code while another thread is in any critical section guarded by the same object. ?

    class Program
{
    static void Main(string[] args)
    {
        TestDifferentCriticalSections();

        Console.ReadLine();
    }

    private static void TestDifferentCriticalSections()
    {
        Test lo = new Test();

        Thread t1 = new Thread(() =>
        {
            lo.MethodA();
        });
        t1.Start();

        Thread t2 = new Thread(() =>
        {
            lo.MethodB();
        });
        t2.Start();
    }
}

public class Test
{
    private object obj = new object();

    public Test()
    { }

    public void MethodA()
    {
        lock (obj)
        {
            for (int i = 0; i < 5; i++)
            {
                Thread.Sleep(500);
                Console.WriteLine("A");
            }
        }
    }

    public void MethodB()
    {
        lock (obj)
        {
            for (int i = 0; i < 5; i++)
            {
                Thread.Sleep(500);
                Console.WriteLine("B");
            }
        }
    }
}
  • 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-31T00:29:57+00:00Added an answer on May 31, 2026 at 12:29 am

    The question is confusingly worded and the answers so far are not particularly clear either. Let me rephrase the question into several questions:

    (1) Does the lock statement ensure that no more than one thread is in the body of the lock statement at any one time?

    No. For example:

    static readonly object lock1 = new object();
    static readonly object lock2 = new object();
    static int counter = 0;
    static object M()
    {
        int c = Interlocked.Increment(ref counter);
        return c % 2 == 0 ? lock1 : lock2;
    }
    
    ...
    lock(M()) { Critical(); }
    

    It is possible for two threads to both be in the body of the lock statement at the same time, because the lock statement locks on two different objects. Thread Alpha can call M() and get lock1, and then thread Beta can call M() and get lock2.

    (2) Assuming that my lock statement always locks on the same object, does a lock statement ensure that no more than one “active” thread is in the body of the lock at any one time?

    Yes. If you have:

    static readonly object lock1 = new object();
    ...
    lock(lock1) { Critical(); }
    

    then thread Alpha can take the lock, and thread Beta will block until the lock is available before entering the lock body.

    (3) Assuming that I have two lock statements, and both lock statements lock on the same object every time, does a lock statement ensure that no more than one “active” thread is in the body of either lock at any one time?

    Yes. If you have:

    static readonly object lock1 = new object();
    ...
    static void X() 
    {
        lock(lock1) { CriticalX(); }
    }
    static void Y() 
    {
        lock(lock1) { CriticalY(); }
    }
    

    then if thread Alpha is in X and takes the lock, and thread Beta is in Y, then thread Beta will block until the lock is available before entering the lock body.

    (4) Why are you putting “active” in “scare quotes”?

    To call attention to the fact that it is possible for a waiting thread to be in the lock body. You can use the Monitor.Wait method to “pause” a thread that is in a lock body, and allow a blocked thread to become active and enter that lock body (or a different lock body that locks the same object). The waiting thread will stay in its “waiting” state until pulsed. At some time after it is pulsed, it rejoins the “ready” queue and blocks until there is no “active” thread in the lock. It then resumes at the point where it left off.

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

Sidebar

Related Questions

I have been playing with the demo code from this msdn article by Jeffrey
I am following this tutorial from MSDN. There's something I saw in the code
this is a code sample from Microsoft(MSDN) (build sql dependency application) , could you
I am reading from MSDN this: A child window has only one parent window,
i took this code from msdn string connString = Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;; using
From this sample code from MSDN http://msdn.microsoft.com/en-us/library/system.string.gethashcode.aspx The hash code for abc is: 536991770
Why in this example from MSDN, in GetEnumerator method, PeopleEnum returns IEnumerator ? public
I've tried this example directly from MSDN: Dim Screens() As System.Windows.Forms.Screens and I can't
This is an example xml from MSDN <?xml version=1.0?> <!-- A fragment of a
I am trying to follow this example from MSDN: http://msdn.microsoft.com/en-us/library/a1hetckb.aspx I think i'm doing

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.