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

  • Home
  • SEARCH
  • 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 8272249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:04:32+00:00 2026-06-08T07:04:32+00:00

After reading about LazyInitializer that it’s : It offers another mode of initialization that

  • 0

After reading about LazyInitializer that it’s :

It offers another mode of initialization that has multiple threads
race to initialize.

here is a sample :

Expensive _expensive;
public Expensive Expensive
     {
       get // Implement double-checked locking
         {
           LazyInitializer.EnsureInitialized (ref _expensive,() => new Expensive());
           return _expensive;
         }
     }

Question #1

looking at :
enter image description here

why #A says its implements double-checking locking ? it is just a get proeprty ?

Question #2

Does #B (lambda expression) is thread safe ?

Question #3

So i searched about this “race-to-initialize” thing by looking at a sample:

volatile Expensive _expensive;
public Expensive Expensive
{
  get
  {
    if (_expensive == null)
    {
      var instance = new Expensive();
      Interlocked.CompareExchange (ref _expensive, instance, null);
    }
    return _expensive;
  }
}

and then i thought about : isnt race to initialize is thread safe ?

e/g/ if 2 threads get into :

enter image description here

the expensive object will be created twice !

So again , 3 question

1)why #A says its implements double-checking locking ? it is just a get proeprty ?

2)Does #B (lambda expression) is thread safe ?

3)isnt race to initialize is thread safe

  • 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-08T07:04:32+00:00Added an answer on June 8, 2026 at 7:04 am

    There are various overloads of EnsureInitialized. Some accepts a synclock object (that can be null and will be created by the EnsureInitialized method). Others don’t have a synclock as a parameter. All the EnsureInitialized guarantee that if called at the same time by two (or more) different threads while the object is uninitialized, the two threads will receive back a reference to the same object. So:

    Expensive _expensive;
    
    // On thread 1
    LazyInitializer.EnsureInitialized (ref _expensive,() => new Expensive());
    
    // On thread 2
    LazyInitializer.EnsureInitialized (ref _expensive,() => new Expensive());
    

    the _expensive object that will be seen by the two threads will be the same.

    The only problem is that new Expensive() could be called twice (once per thread, so in a multi-thread race it could be called even more times.)

    If you don’t want it, use the synclock overload:

    Expensive _expensive;
    object _sync = null;
    bool _useless;
    
    // On thread 1
    LazyInitializer.EnsureInitialized (ref _expensive, ref useless, ref _sync, () => new Expensive());
    
    // On thread 2
    LazyInitializer.EnsureInitialized (ref _expensive, ref useless, ref _sync, () => new Expensive());
    

    Now the new Expensive() will be called only once, for every possible combination of the two (or more) threads running.

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

Sidebar

Related Questions

After reading about the System.Diagnostics.Contracts.Contract static class that has been influenced by the awesomeness
After reading about CCR : http://www.infoq.com/news/2008/12/CCR I get the impression that it does pretty
After reading about Erlang's lighweight processes I was pretty much sure that they were
After reading about dynamic class loading (that is, loading a .class file from disk)
after reading about sequence points, I learned that i = ++i is undefined. So
After reading about attr_accessible in the Rails 3.1 API , I see that there
I have an application that was mapped using fields, so after reading about it
After reading about the problem of passing empty std::string objects between DLLs and EXEs,
After reading a description about swapping pointer addresses on Stackoverflow, I have a question
After reading an article about fonts, i tried the code and i am getting

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.