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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:42:25+00:00 2026-05-20T19:42:25+00:00

I have two internal properties that use lazy-loading of backing fields, and are used

  • 0

I have two internal properties that use lazy-loading of backing fields, and are used in a multi-threaded application, so I have implemented a double-checking lock scheme as per this MSDN article

Now, firstly assuming that this is an appropriate pattern, all the examples show creating a single lock object for an instance. If my two properties are independent of each other, would it not be more efficient to create a lock instance for each property?

It occurs to me that maybe there is only one in order to avoid deadlocks or race-conditions. A obvious situation doesn’t come to mind, but I’m sure someone can show me one… (I’m not very experienced with multi-threaded code, obviously)

private List<SomeObject1> _someProperty1;
private List<SomeObject2> _someProperty2;
private readonly _syncLockSomeProperty1 = new Object();
private readonly _syncLockSomeProperty2 = new Object();

internal List<SomeObject1> SomeProperty1
{
  get
  {
    if (_someProperty1== null)
    {
      lock (_syncLockSomeProperty1)
      {
        if (_someProperty1 == null)
        {
          _someProperty1 = new List<SomeObject1>();
        }
      }
    }
    return _someProperty1;
  }

  set
  {
    _someProperty1 = value;
  }
}

internal List<SomeObject2> SomeProperty2
{
  get
  {
    if (_someProperty2 == null)
    {
      lock (_syncLockSomeProperty2)
      {
        if (_someProperty2 == null)
        {
          _someProperty2 = new List<SomeObject2>();
        }
      }
    }
    return _someProperty2;
  }

  set
  {
    _someProperty2 = value;
  }
}
  • 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-20T19:42:26+00:00Added an answer on May 20, 2026 at 7:42 pm

    Yes, if they are independent of each other, this would indeed be more efficient, as access to one wont’ block access to the other. You’re also on the money about the risk of a deadlock if that independence turned out to be false.

    The question is, presuming that _someProperty1 = new List<SomeObject1>(); isn’t the real code for assigning to _someProperty1 (hardly worth the lazy-load, is it?), then the question is: Can the code that fills SomeProperty1 ever call that which fills SomeProperty2, or vice-versa, through any code-path, no matter how bizarre?

    Even if one can call the other, there can’t be a deadlock, but if they both can call each other (or 1 call 2, 2 call 3 and 3 call 1, and so on), then a deadlock can definitely happen.

    As a rule, I’d start with broad locks (one lock for all locked tasks) and then make the locks narrower as an optimisation as needed. In cases where you have, say, 20 methods which need locking, then judging the safety can be harder (also, you begin to fill memory just with lock objects).

    Note that there are two issues with your code also:

    First, you don’t lock in your setter. Possibly this is fine (you just want your lock to prevent multiple heavy calls to the loading method, and don’t actually care if there are over-writes between the set, and the get), possibly this is a disaster.

    Second, depending on the CPU running it, double-check as you write it can have issues with read/write reordering, so you should either have a volatile field, or call a memory barrier. See http://blogs.msdn.com/b/brada/archive/2004/05/12/130935.aspx

    Edit:

    It’s also worth considering whether it’s really needed at all.

    Consider that the operation itself should be thread-safe:

    1. Do a bunch of stuff is done.
    2. Have an object created based on that bunch of stuff.
    3. Assign that object to the local variable.

    1 and 2 will only happen on one thread, and 3 is atomic. Therefore, the advantage of locking is:

    1. If performing step 1 and/or 2 above have their own threading issues, and aren’t protected from them by their own locks, then locking is 100% necessary.

    2. If it would be disastrous for something to have acted upon a value obtained in step 1 and 2, and then later to do so with step 1 and 2 being repeated, locking is 100% necessary.

    3. Locking will prevent the waste of 1 and 2 being done multiple times.

    So, if we can rule out case 1 and 2 as an issue (takes a bit of analysis, but it’s often possible), then we’ve only preventing the waste in case 3 to worry about. Now, maybe this is a big worry. However, if it would rarely come up, and also not be that much of a waste when it did, then the gains of not locking would outweigh the gains of locking.

    If in doubt, locking is probably the safer approach, but its possible that just living with the occasional wasted operation is better.

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

Sidebar

Related Questions

I have two application that need to talk to each other. App1 needs to
I have problem with using next two properties together in one Air application, I
In my application I have an object with several properties that get set in
I have a container div that holds two internal divs; both should take 100%
Lets say i have two classes A and B, which have theirs internal state
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two stored procedures I wish to use in my stored procedure, but
I have two custom attributes defined like so: internal class SchemaAttribute : Attribute {
We have two mediawiki (like wikipedia) installations. One is public and one is internal.
If I have two internal computers connecting to the same external IP address through

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.