Static Functions and Thread Safety. How does it work?

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

    Static Functions and Thread Safety. How does it work?

    Hello,
    It might seem like a stupid question to some. But I need to put this issue
    to rest once and for all, since it keeps coming up in code reviews every now
    and then.
    There’s a static function in business logic class invoked by a Web
    multi-user application. Each request calls this static function passing in
    one unique ID, and XML serialized object (as out param), function
    deserializes XML to object, creates new instances of DB Access object and
    makes calls to database based on ID, modifies object’s properties and
    serializes it back to be returned as out param.
    Now there are concerns coming from some developers that it’s not
    thread-safe, that static function might cause race-conditions, if
    simultaneous calls are made to the static function there will be "detrimenta l
    consequences".

    Are the any real thread safety/race conditions issues with static functions?
    Can someone tell me or point to a good link “behind the scenes” how static
    function get invoked:
    1. Will each call get its own stack?
    2. How simultaneous calls are processed?

  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Static Functions and Thread Safety. How does it work?

    Hi,

    If the method DOES NOT modify or use a variable external to it you are ok.
    Based on your escenario you should be ok as all the instances you use are
    created inside the method (so each call will have its own instances). The
    same applies with the parameters

    [color=blue]
    > 1. Will each call get its own stack?[/color]

    Of course, each time you call a method (no matter if static, virtual,
    regular) a new stack frame is created. so as I said above if you use no
    instance from outside the method you are ok
    [color=blue]
    > 2. How simultaneous calls are processed?[/color]

    Each call will create a new thread that you execute your page. All this
    thread are part of the same application ( AppDomain)



    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation


    Comment

    • Karl Seguin [MVP]

      #3
      Re: Static Functions and Thread Safety. How does it work?

      I think ignacio answered this properly, but I wanted to re-iterate.

      Local variables declared INSIDE a static function aren't static (unless you
      declare them so).

      For example:

      public void static DoSomething()
      {
      int i = 0;
      ++i;
      }

      Each call to DoSoemthing (even if they happen at exactly the same time) will
      create their own i. If you declare i as static/shared also, then it's a
      different story (but that's true whether or not the method itself is
      static).

      Karl

      --
      Programming blog exploring Zig, Elixir, Go, Testing, Design and Performance




      "WebMatrix" <WebMatrix@disc ussions.microso ft.com> wrote in message
      news:F1B0B30E-8ECC-476F-9E9F-34265B14DA2A@mi crosoft.com...[color=blue]
      > Hello,
      > It might seem like a stupid question to some. But I need to put this issue
      > to rest once and for all, since it keeps coming up in code reviews every
      > now
      > and then.
      > There's a static function in business logic class invoked by a Web
      > multi-user application. Each request calls this static function passing in
      > one unique ID, and XML serialized object (as out param), function
      > deserializes XML to object, creates new instances of DB Access object and
      > makes calls to database based on ID, modifies object's properties and
      > serializes it back to be returned as out param.
      > Now there are concerns coming from some developers that it's not
      > thread-safe, that static function might cause race-conditions, if
      > simultaneous calls are made to the static function there will be
      > "detrimenta l
      > consequences".
      >
      > Are the any real thread safety/race conditions issues with static
      > functions?
      > Can someone tell me or point to a good link "behind the scenes" how static
      > function get invoked:
      > 1. Will each call get its own stack?
      > 2. How simultaneous calls are processed?
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Static Functions and Thread Safety. How does it work?

        <"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO .
        ANDME net>> wrote:[color=blue]
        > I think ignacio answered this properly, but I wanted to re-iterate.
        >
        > Local variables declared INSIDE a static function aren't static (unless you
        > declare them so).
        >
        > For example:
        >
        > public void static DoSomething()
        > {
        > int i = 0;
        > ++i;
        > }
        >
        > Each call to DoSoemthing (even if they happen at exactly the same time) will
        > create their own i. If you declare i as static/shared also, then it's a
        > different story (but that's true whether or not the method itself is
        > static).[/color]

        You can't declare local variables as static in C# anyway...

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Karl Seguin [MVP]

          #5
          Re: Static Functions and Thread Safety. How does it work?

          I honestly didn't know that. I do know you can do it in vb.net...how
          interesting....



          --
          Programming blog exploring Zig, Elixir, Go, Testing, Design and Performance




          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.1e8e45 885ef3677e98cfa 8@msnews.micros oft.com...[color=blue]
          > <"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO .
          > ANDME net>> wrote:[color=green]
          >> I think ignacio answered this properly, but I wanted to re-iterate.
          >>
          >> Local variables declared INSIDE a static function aren't static (unless
          >> you
          >> declare them so).
          >>
          >> For example:
          >>
          >> public void static DoSomething()
          >> {
          >> int i = 0;
          >> ++i;
          >> }
          >>
          >> Each call to DoSoemthing (even if they happen at exactly the same time)
          >> will
          >> create their own i. If you declare i as static/shared also, then it's a
          >> different story (but that's true whether or not the method itself is
          >> static).[/color]
          >
          > You can't declare local variables as static in C# anyway...
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Ignacio Machin \( .NET/ C# MVP \)

            #6
            Re: Static Functions and Thread Safety. How does it work?

            Hi,

            "Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
            net> wrote in message news:%23HD9dB2T GHA.5108@TK2MSF TNGP11.phx.gbl. ..[color=blue]
            >I think ignacio answered this properly, but I wanted to re-iterate.
            >
            > Local variables declared INSIDE a static function aren't static (unless
            > you declare them so).
            >
            > For example:
            >
            > public void static DoSomething()
            > {
            > int i = 0;
            > ++i;
            > }
            >
            > Each call to DoSoemthing (even if they happen at exactly the same time)
            > will create their own i. If you declare i as static/shared also, then it's
            > a different story (but that's true whether or not the method itself is
            > static).[/color]

            AFAIK you cannot declare static variables inside a method , not sure in 2.0
            but I would bet its the same



            --
            Ignacio Machin,
            ignacio.machin AT dot.state.fl.us
            Florida Department Of Transportation


            Comment

            • jeff

              #7
              Re:Static Functions and Thread Safety. How does it work?

              The static function itself is OK, but it's the code inside that sounds like
              it would be the culprit of concern. Fundamentally static functions are fine
              to work with.

              read all about static classes and methods at microsoft.

              Comment

              Working...