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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:27:20+00:00 2026-05-30T08:27:20+00:00

I was reading this article about thread safety in Singletons, and it got me

  • 0

I was reading this article about thread safety in Singletons, and it got me thinking that I don’t understand the lock method.

In the second version, the author has this:

public sealed class Singleton
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}

Whereas I would’ve done something more like this:

public sealed class Singleton
{
    private static Singleton instance = null;

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            lock (instance)
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}

Why would you use a padlock object, rather than locking the actual object you want to lock?

  • 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-30T08:27:21+00:00Added an answer on May 30, 2026 at 8:27 am

    What would you expect to happen the first time you accessed the Instance property, before you’ve got an object to lock on?

    (Hint: lock(null) goes bang…)

    As a separate measure, I almost always avoid locking on “the actual object” – because typically there may well be other code which that reference is exposed to, and I don’t necessarily know what that’s going to lock on. Even if your version did work, what would happen if some external code wrote:

    // Make sure only one thread is ever in this code...
    lock (Singleton.Instance)
    {
        // Do stuff
    }
    

    Now no-one else can even get the instance while that code is executing, because they’ll end up blocked in the getter. The lock in the getter isn’t meant to be guarding against that – it’s only meant to be guarding against multiple access within the getter.

    The more tightly you can keep control of your locks, the easier it is to reason about them and to avoid deadlocks.

    I very occasionally take a lock on a “normal” object if:

    • I’m not exposing that reference outside that class
    • I have confidence that the type itself (which will always have a reference to this, of course) won’t lock on itself.

    (All of this is a reason to avoid locking on this, too, of course…)

    Fundamentally, I believe the idea of allowing you to lock on any object was a bad idea in Java, and it was a bad move to copy it in .NET 🙁

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

Sidebar

Related Questions

After reading this article on thedailywtf.com, I'm not sure that I really got the
I was reading this article about Dynamic Objects in C# 4.0 . In that
I was reading this article about the module Javascript pattern , but I don't
I was reading this article about relative time calculation The problem is that the
I was reading this article about Double-Checked locking and out of the main topic
I was reading this article by Brandon Aaron here , about how jquery context
I was just reading this article and it mentions that some organization had an
I was under the impression, after reading this article that it is better to
I was reading this MSDN article about the usage of properties and methods in
I was reading this article , and this guy goes on talking about how

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.