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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:18:59+00:00 2026-05-27T07:18:59+00:00

I took the code of DCL from Joe Duffy’s book ‘Concurrent programming on windows’

  • 0

I took the code of DCL from Joe Duffy’s book ‘Concurrent programming on windows’

class LazyInit<T> where T : class
{
private volatile T m_value;
private object m_sync = new object();
private Func<T> m_factory;
public  LazyInit(Func<T>  factory)  {  m_factory  =  factory;  }
public T value
{
  get
  {
    if (m_value == null)
    {
      lock (m_sync)
      {
        if (m_value == null)
        {
          m_value = m_factory();
        }
      }
    }
    return m_value;
  }
}
}

it is said marking m_value volatile can prevent writes reordering that will leads to other threads getting ‘non null object with uninitialized fields’. If the problem happens just because the possible writes reordering, can I just use ‘Volatile Write’ instead of marking the filed volatile, like below? (This code looks a little awkward for demonstration, I just want to make sure if we can only use volatile write instead)

class LazyInit<T> where T : class
{
private object m_value;
private object m_sync = new object();
private Func<T> m_factory;
public  LazyInit(Func<T>  factory)  {  m_factory  =  factory;  }
public T value
{
  get
  {
    if (m_value == null)
    {
      lock (m_sync)
      {
        if (m_value == null)
        {
          Thread.VolatileWrite(ref m_value, m_factory());
        }
      }
    }
    return (T)m_value;
  }
}
}

A related question is the Interlocked version from the book

class LazylnitRelaxedRef<T> where T : class
{
private volatile T m_value;
private Func<T> m_factory;
public LazylnitRelaxedRef(Func<T> factory) { m_factory = factory; }
public T Value
{
  get
  {
    if (m_value == null)
      Interlocked.CompareExchange(ref  m_value, m_factory(), null);
    return m_value;
  }
}
}

Since the ECMA-CLI specs the ‘Interlocked operation perform implicit acquire/release operations’, do we still need volatile in this case?

  • 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-27T07:18:59+00:00Added an answer on May 27, 2026 at 7:18 am

    First, messing with volatile is really hard, so don’t get too loose with it! But, here is a really close answer to your question, and here is an article that I think everyone should read before using the keyword volatile, and definitely before starting to use VolatileRead, VolatileWrite and MemoryBarrier.

    The answer in the first link is: no you don’t need to use volatile, you just need to use System.Threading.Thread.MemoryBarrier() RIGHT BEFORE you assign the new value. This is because the release_fence implied when using the volatile keyword makes sure that it gets finished writing out to the main memory, and that no read/write operations can be performed until it’s finished.

    So, what does Thread.VolatileWrite() do, and does it perform the same functions that we get from the ‘volatile’ keyword? Well, here’s the full code from this function:

    public static void VolatileWrite (ref int address, int value)
    {
      MemoryBarrier(); address = value;
    }
    

    Yes, it calls MemoryBarrier right before it assigns your value, which is sufficient!

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

Sidebar

Related Questions

I took the following code from the examples page on Asio class tcp_connection :
I took some code from my C# Windows form app which uses the Application
I took the code from this example. http://msdn.microsoft.com/en-us/library/2tw134k3.aspx What I am wondering (and I've
I took the code from the google-analytics website and put it right before the
i took this code from a different question and my script file has more
I took some code from some questions here in SO as well as some
I took some code from the internet that searches data entered in a table
i took this code from msdn string connString = Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;; using
EDIT -- took the code from below and made it so it can handle
I recently took some code from Delphi 2007 and upgraded it to Delphi 2009.

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.