Singleton Thread-Safety Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cool Guy

    #1

    Singleton Thread-Safety Question

    In the following implementation of Singleton, is the instance constructor
    guaranteed to execute only once regardless of how many threads try to
    access it at one time?

    class Test {
    public static readonly Test Instance = new Test();

    Test() {
    // set up state. this mustn't execute more than once
    }
    }
  • Jon Skeet [C# MVP]

    #2
    Re: Singleton Thread-Safety Question

    Cool Guy <coolguy@abc.xy z> wrote:[color=blue]
    > In the following implementation of Singleton, is the instance constructor
    > guaranteed to execute only once regardless of how many threads try to
    > access it at one time?
    >
    > class Test {
    > public static readonly Test Instance = new Test();
    >
    > Test() {
    > // set up state. this mustn't execute more than once
    > }
    > }[/color]

    Yes, that's guaranteed to only execute once. There are some caveats
    about ordering when there's potential for a deadlock, but in simple
    situations like the above (which 99% of static initializers fall into)
    it's fine.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Cool Guy

      #3
      Re: Singleton Thread-Safety Question

      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote:
      [color=blue][color=green]
      >> class Test {
      >> public static readonly Test Instance = new Test();
      >>
      >> Test() {
      >> // set up state. this mustn't execute more than once
      >> }
      >> }[/color]
      >
      > Yes, that's guaranteed to only execute once. There are some caveats
      > about ordering when there's potential for a deadlock, but in simple
      > situations like the above (which 99% of static initializers fall into)
      > it's fine.[/color]

      Could you give an example of one such caveat?

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Singleton Thread-Safety Question

        Cool Guy <coolguy@abc.xy z> wrote:[color=blue][color=green]
        > > Yes, that's guaranteed to only execute once. There are some caveats
        > > about ordering when there's potential for a deadlock, but in simple
        > > situations like the above (which 99% of static initializers fall into)
        > > it's fine.[/color]
        >
        > Could you give an example of one such caveat?[/color]

        Sure. It's not actually a matter of initializing twice, but of the
        incompletely initialized state being visible temporarily. For instance:

        using System;

        public class A
        {
        static string Something = B.Bar;
        public static string Foo = "Hello";
        }

        public class B
        {
        public static string Bar = "There";

        static B()
        {
        Console.WriteLi ne ("In B, A.Foo='{0}'", A.Foo);
        }
        }

        public class Test
        {
        static void Main()
        {
        Console.WriteLi ne (A.Foo);
        }
        }

        The results are:
        In B, A.Foo=''
        Hello

        There are further rules for multithreaded system - I suggest you look
        in the CLI spec, section 9.5.3.3 of partition 1 for further details.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        Working...