Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Edit:</h3> <blockquote> <p>I meant to said that once I have called Singleton.Instance.abc(); Singleton.Instance should not be null until it is disposed manually. Right?</p> </blockquote> <p>Once a single thread has been used to access the Singleton it will already be initialized, and any threads you kick off after that call can safely access the Singleton without it ever being <code>null</code>.</p> <p>If you kick off multiple threads that access the Singleton at the same time, and leave your code as-is, you will have a problem. If you don't use thread synchronization (<code>lock</code>) to make sure the <code>if null</code> block is entered only one-thread-at-a-time, then you would have a race condition.</p> <p>If multiple threads sneak by the <code>if</code> block, then multiple threads would try to initialize <code>instance</code>. This is messy, and it is hard to tell what exactly will happen. One problem is that you could actually get multiple instances, one for each thread that snuck through. Any subsequent calls (after the race condition) would get the last instance created.</p> <p>This may not be the worst problem you will see, though. There might be even worse problems that cause crashes (not sure). It is best to use thread-safe operations for your Singleton rather than finding out :)</p> <p>See my original answer below for how to make your Singleton thread-safe:</p> <hr> <p>You can create the instance in a static constructor.</p> <p>The static constructor will only be called on first access, so you wouldn't need your lazy initialization code.</p> <pre><code>public sealed class Singleton { private static Singleton instance; private Singleton() { } static Singleton() { instance = new Singleton(); } public static Singleton Instance { get { return instance; } } } </code></pre> <p>According to <a href="https://stackoverflow.com/questions/7095/is-the-c-sharp-static-constructor-thread-safe">the answer to this question</a>, it is guaranteed to be called only once, and is thread safe:</p> <blockquote> <p>Static constructors are guaranteed to be run only once per application domain, before any instances of a class are created or any static members are accessed. <a href="http://msdn.microsoft.com/en-us/library/aa645612.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa645612.aspx</a></p> <p>The implementation shown is thread safe for the initial construction, that is, no locking or null testing is required for constructing the Singleton object. However, this does not mean that any use of the instance will be synchronised. There are a variety of ways that this can be done; I've shown one below.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload