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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:54:02+00:00 2026-05-13T08:54:02+00:00

Altough I am using locks in my app, I do not understand what exactly

  • 0

Altough I am using locks in my app, I do not understand what exactly does locking on particular reference type. I thought it just stops the thread until the content of {} is finished. But I have read that locking(this) is bad, if its public – why? The article explained it but I do not understand sice I do not know what happened to the object itself being locked.
For example, what if I use lock(this) and from another thread call its method? I thought only the code under the lock is protected, or I will be unable to access the locked object at all?
Thanks

  • 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-13T08:54:02+00:00Added an answer on May 13, 2026 at 8:54 am

    Each object on the managed heap can be used as a lock-object, which is a means to synchronize access between threads.

    I thought it just stops the thread until the content of {} is finished.

    Well, a lock it stops other threads from acquiring the lock until the lock is released, which is most commonly at the end of a lock statement (but it could also be a Monitor.Wait).

    The lock(this) usage is dangerous because locking is complex, and understanding exactly which threads are locking which object(s) at which time is very important to avoid deadlocks; however, if you lock(this) you aren’t in control of other threads – which could also be (unexpectedly) locking the same object. It is far safer to use a private field for locking.

    As an example, if you have (in a synchronized list):

    private IList<T> innerList = ...
    public int Count { get { lock(this) { return innerList.Count; } } }
    

    Then it isn’t hard to imagine another bit of code also having a reference to this synchronized list, and locking on it, for example:

    SyncList<T> list = ...
    lock(list) { // lock for atomicity
        if(!list.Contains(value)) list.Add(value);
    }
    

    Which is a potential deadlock; it would be much better if Count didn’t lock(this), but locked a private object, i.e.

    private readonly object syncLock = new object();
    public int Count { get { lock(syncLock) { return innerList.Count; } } }
    

    Now there is no risk of this issue. Another issue here is that both field-like events and [MethodImpl] cause lock(this). Locking on a Type (for static methods) is equally dangerous for exactly the same reasons.

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

Sidebar

Related Questions

Overview I am using CompositeWPF to create an app using C#. This really should
I've just started using both Linq to SQL and Windsor Castle IoC container for
I am trying to migrate a simple WinForms app to a ASP.Net web app.
I'm using com interop to talk to some physical piece of hardware. When I
I currently have a simple iPhone app that loads a custom subclass of UIView.
I'm using an NSArrayController , NSMutableArray and NSTableView to show a list of my
I need to run Perl applications I develop on cygwin Windows on HP unix
So, I'm having some issues with my implementation of the Three20 TTLauncherView. I am
So i have a query that looks like this: SELECT col1, col2, col3 ...

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.