Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Singletons: Static ctors or Monitors?

Singletons: Static ctors or Monitors?

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpdotnetjsonquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Heath Stewart
    wrote on last edited by
    #1

    In a recent thread on the ndoc development list (for which I contribute), we were talking about the documentation of static constructors and that got me to thinking: In most of my work, I use the approach for creating singleton objects by makeing a private constructor, then having a static property that returns an instance from which I can call other public or internal methods or properties. The whole thing looks like this:

    public class Test
    {
    private string text;
    private static Test instance;
    private static volatile object syncRoot = new Object();
    private Test()
    {
    this.text = "Test";
    }
    public static Test Instance
    {
    get
    {
    if (instance == null)
    lock (syncRoot)
    if (instance == null)
    instance = new Test();
    return instance;
    }
    }
    public string Text
    {
    get { return this.text; }
    }
    }

    This seems to be the method that Microsoft uses in the .NET BCL. And it works good. I do use static constructors in some things that aren't so sensative (like wrapping some GetDevice Win32 API code). What I'm wondering is if static constructors work just as good as the method above? I haven't been able to find any documentation that discusses this in any detail, but my first reaction is that the first method is better because monitors are used and if would be thread-safe. But does the CLR invoke static constructors in a thread-safe manner on its own?

    Reminiscent of my younger years...

    10 LOAD "SCISSORS" 20 RUN

    A 1 Reply Last reply
    0
    • H Heath Stewart

      In a recent thread on the ndoc development list (for which I contribute), we were talking about the documentation of static constructors and that got me to thinking: In most of my work, I use the approach for creating singleton objects by makeing a private constructor, then having a static property that returns an instance from which I can call other public or internal methods or properties. The whole thing looks like this:

      public class Test
      {
      private string text;
      private static Test instance;
      private static volatile object syncRoot = new Object();
      private Test()
      {
      this.text = "Test";
      }
      public static Test Instance
      {
      get
      {
      if (instance == null)
      lock (syncRoot)
      if (instance == null)
      instance = new Test();
      return instance;
      }
      }
      public string Text
      {
      get { return this.text; }
      }
      }

      This seems to be the method that Microsoft uses in the .NET BCL. And it works good. I do use static constructors in some things that aren't so sensative (like wrapping some GetDevice Win32 API code). What I'm wondering is if static constructors work just as good as the method above? I haven't been able to find any documentation that discusses this in any detail, but my first reaction is that the first method is better because monitors are used and if would be thread-safe. But does the CLR invoke static constructors in a thread-safe manner on its own?

      Reminiscent of my younger years...

      10 LOAD "SCISSORS" 20 RUN

      A Offline
      A Offline
      Andy Smith
      wrote on last edited by
      #2

      acorrding to this article, the best way is thus:// .NET Singleton sealed class Singleton { private Singleton() {} public static readonly Singleton Instance = new Singleton(); }
      as for your threading question: "What about thread-safe initialization? The Framework addresses this too. The Framework internally guarantees thread safety on static type initialization."

      H 1 Reply Last reply
      0
      • A Andy Smith

        acorrding to this article, the best way is thus:// .NET Singleton sealed class Singleton { private Singleton() {} public static readonly Singleton Instance = new Singleton(); }
        as for your threading question: "What about thread-safe initialization? The Framework addresses this too. The Framework internally guarantees thread safety on static type initialization."

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        Essentially, this is the same as using a static constructor. All statics that are initialized are done so in the .cctor specialname (the static constructor). Using the above syntax is merely a shorthand. So, you've actually described the same process. The first method I described and coded doesn't rely about a static constructor except for the synchronization object, which is only used by the Monitor and is not indicative of the singleton itself. I appreciate your answer though. The latter part was good to know. I've always suspected based on evidence, but I've never been able to find it in the documentation which I've read from start to finish several times. (and, oh, how the ending makes me cry! :P)

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        L 1 Reply Last reply
        0
        • H Heath Stewart

          Essentially, this is the same as using a static constructor. All statics that are initialized are done so in the .cctor specialname (the static constructor). Using the above syntax is merely a shorthand. So, you've actually described the same process. The first method I described and coded doesn't rely about a static constructor except for the synchronization object, which is only used by the Monitor and is not indicative of the singleton itself. I appreciate your answer though. The latter part was good to know. I've always suspected based on evidence, but I've never been able to find it in the documentation which I've read from start to finish several times. (and, oh, how the ending makes me cry! :P)

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Just remmeber static is only limited to its appdomain. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups