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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:25:29+00:00 2026-06-09T17:25:29+00:00

Some newbie questions about multi-threading in .NET which I think will help reinforce some

  • 0

Some newbie questions about multi-threading in .NET which I think will help reinforce some concepts I’m trying to absorb – I’ve read several multi-threading material (including the Albahari ebook) but feel I just need some confirmation of some questions to help drive these concepts home

  1. A lock scope protects a shared region of code – suppose there is a thread executing a method that increments a simple integer variable x in a loop – however this won’t protect code elsewhere that might also alter variable x eg in another method on another thread …

    Since this is two different regions of code potentially affecting the same variable, do we solve this by locking both regions of code using the same lock variable for both lock scopes around variable x? If you locked both regions of code with different lock variables, this would not protect the variable correct?

  2. To further this example, using the same lock variable, what would happen if for some reason, code in one method went into some infinite loop and never relinquished the lock variable – how could the second region of code in the other method detect this?

  3. How does the choice of lock variable influence the behavior of the lock? I’ve read numerous posts on this subject already but can never seem to find a definitive answer – in some instances people explicitly use an object variable specifically for this purpose, other times people use lock(this) and finally there’ve been times I’ve seen people use a type object.

    How do the different choices of lock variables influence the behavior / scope of the lock and what scenarios would it make sense to use one over the other?

  4. suppose you have a hashtable wrapped in a class exposing add, remove, get and some sort of Calculate method (say each object represents a quantity and this method sums each value) and all these methods are locked – however, once a reference to an object in that collection is made available to other code and passed around an application, this object (not the hashtable) would now be outside the lock scope surrounding the methods of that class ..how could you then protect access / updates to those actual objects taken from the hashtable, which could interfere with the Calculate method?

Appreciate any heuristics provided that would help reinforce these concepts for me – 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-06-09T17:25:30+00:00Added an answer on June 9, 2026 at 5:25 pm

    1)
    You are correct. You must use the same lock object to protect two distinct area’s of code that for example increment the variable x.

    2)
    This is known as a deadlock and is one of the difficulties with multithreaded programming. There are algorithms which can be used to prevent deadlocks such as the Bankers Algorithm.

    3)
    Some languages make locking easy, for example in .Net you can just create an object and use it as the shared lock. This is good for synchronising code within a given process. Lock(this) just applies the lock to the object in question. However try to avoid this, instead create a private object and use that. Lock(this) can lead to deadlocking situations. The lock object underneath is probably just a wrapper around a Critical Section. If you wanted to protect a resource across different processes you would need a much heavier named Mutex, this requires a lock on a kernel object and is expensive, so do not use unless you must.

    4)You need to make sure locking is applied there as well. But surely when people call methods on this reference they call the methods which employ synchronisation.

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

Sidebar

Related Questions

I'm brand new to SQL Server 2008, and have some newbie questions about the
I'm a newbie that needs some help on getting the AVD running with my
Im a javascript newbie and would need some help with this simple thing... I
I am newbie in Ruby.. seeking some help.. I have a code DB =
I'm a complete newbie when it comes to servers, so I need some help.
I'm learning vba today, I'm a newbie and I have a few questions about
As a quazi-newbie to asp.net, I have a question about session variables. I'm building
I read some useful posts here on SO about previous maven questions, I'm currently
I did some homework and while I can answer all below questions about exporting
I'm learning ruby and have a few questions about some code I wrote for

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.