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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:33:23+00:00 2026-05-13T07:33:23+00:00

Multiple texts say that when implementing double-checked locking in .NET the field you are

  • 0

Multiple texts say that when implementing double-checked locking in .NET the field you are locking on should have volatile modifier applied. But why exactly? Considering the following example:

public sealed class Singleton
{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Singleton() {}

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

         return instance;
      }
   }
}

why doesn’t “lock (syncRoot)” accomplish the necessary memory consistency? Isn’t it true that after “lock” statement both read and write would be volatile and so the necessary consistency would be accomplished?

  • 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-13T07:33:23+00:00Added an answer on May 13, 2026 at 7:33 am

    Volatile is unnecessary. Well, sort of**

    volatile is used to create a memory barrier* between reads and writes on the variable.
    lock, when used, causes memory barriers to be created around the block inside the lock, in addition to limiting access to the block to one thread.
    Memory barriers make it so each thread reads the most current value of the variable (not a local value cached in some register) and that the compiler doesn’t reorder statements. Using volatile is unnecessary** because you’ve already got a lock.

    Joseph Albahari explains this stuff way better than I ever could.

    And be sure to check out Jon Skeet’s guide to implementing the singleton in C#

    update:
    *volatile causes reads of the variable to be VolatileReads and writes to be VolatileWrites, which on x86 and x64 on CLR, are implemented with a MemoryBarrier. They may be finer grained on other systems.

    **my answer is only correct if you are using the CLR on x86 and x64 processors. It might be true in other memory models, like on Mono (and other implementations), Itanium64 and future hardware. This is what Jon is referring to in his article in the “gotchas” for double checked locking.

    Doing one of {marking the variable as volatile, reading it with Thread.VolatileRead, or inserting a call to Thread.MemoryBarrier} might be necessary for the code to work properly in a weak memory model situation.

    From what I understand, on the CLR (even on IA64), writes are never reordered (writes always have release semantics). However, on IA64, reads may be reordered to come before writes, unless they are marked volatile. Unfortuantely, I do not have access to IA64 hardware to play with, so anything I say about it would be speculation.

    i’ve also found these articles helpful:
    http://www.codeproject.com/KB/tips/MemoryBarrier.aspx
    vance morrison’s article (everything links to this, it talks about double checked locking)
    chris brumme’s article (everything links to this)
    Joe Duffy: Broken Variants of Double Checked Locking

    luis abreu’s series on multithreading give a nice overview of the concepts too
    http://msmvps.com/blogs/luisabreu/archive/2009/06/29/multithreading-load-and-store-reordering.aspx
    http://msmvps.com/blogs/luisabreu/archive/2009/07/03/multithreading-introducing-memory-fences.aspx

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

Sidebar

Ask A Question

Stats

  • Questions 355k
  • Answers 355k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are only two types of resources I can think… May 14, 2026 at 8:35 am
  • Editorial Team
    Editorial Team added an answer Yes: Add the derived column component to the dataflow, then… May 14, 2026 at 8:35 am
  • Editorial Team
    Editorial Team added an answer There's probably a clever CSS way to do this, but… May 14, 2026 at 8:35 am

Related Questions

I will soon be beginning work on a project that (from the spec) reminds
I've been tasked with implementing a Date/Time selector for several areas of our web
Most of our Eclipse projects have multiple source folders, for example: src/main/java src/test/java When
If a form is submitted but not by any specific button, such as by
I've got a MySQL query somewhat like the following: SELECT * FROM products LEFT

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.