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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:16:18+00:00 2026-06-05T07:16:18+00:00

When some thread locks myList in SomeMethodA and while executing the block inside lock

  • 0

When some thread locks myList in SomeMethodA and while executing the block inside lock, does other thread can execute myList.Add(1) in SomeMethodB or it will wait because ‘myList’ is locked in SomeMethodA?

class A
{
    private List<int> myList;

    public void SomeMethodA()
    {
       lock(myList)
       {
          //...
       }
    }

    public void SomeMethodB()
    {
       myList.Add(1);
    }
}
  • 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-06-05T07:16:19+00:00Added an answer on June 5, 2026 at 7:16 am

    Edit Explicit answer: No, you need to lock the list explicitely in the SomeMethodB. The compiler will not automatically add locks for you

    • why would you ever have to explicitely lock otherwise?
    • things would be horribly slow. It would be far better to just forbid multi threading than to always lock each object access1

    The recommended idiom is this:

    class A
    {
        private List<int> myList;
        private readonly object _lockObject = new Object();
    
        public void SomeMethodA()
        {
           lock(_lockObject)
           {
              //...
           }
        }
    
        public void SomeMethodB()
        {
           lock(_lockObject)
           {
               myList.Add(1);
           }
        }
    }
    

    Beware of exposing finegrained locking like that (you’d typically want to do coarsegrained locking as long as no blocking operations can occur under the lock).

    Note Locks in C# are reentrant, though, so calling SomeMethodB from within the lock in SomeMethodA will not deadlock

    Update Rationale behind using a private lock object instance:

    In general, avoid locking on a public type, or instances beyond your code’s control. The common constructs lock (this), lock (typeof (MyType)), and lock (“myLock”) violate this guideline:

    • lock (this) is a problem if the instance can be accessed publicly.
    • lock (typeof (MyType)) is a problem if MyType is publicly accessible.
    • lock("myLock") is a problem because any other code in the process using the same string, will share the same lock.

    Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances.

    See: http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx


    1 (aside from other issues with that approach, such as null values, reference updates, deadlocks etc.)

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

Sidebar

Related Questions

How can i do this ? I tryed with some thread but nothing happened.
I'm working on an application that has a software watchdog. If some thread locks
I have a java code that looks like this: //UI thread //Some code Job
When i start some thread in my program, everything else is stopped. This is
I have some Thread count pCount and I have some float[] array. I want
So, as soon as I hit a breakpoint in some thread, is it possible
I have a background thread and the thread calls some methods that update the
Hi all i have a background worker thread and some unmanaged code dlls ,
I'm working on a Windows Service that's having some issues with Thread.Sleep() so I
This is a short question. At some point my thread understand that it should

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.