Static Functions and Thread Safety. How does it work?

Discussion in 'ASP .Net' started by =?Utf-8?B?V2ViTWF0cml4?=, Mar 24, 2006.

  1. 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 "detrimental
    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?
    =?Utf-8?B?V2ViTWF0cml4?=, Mar 24, 2006
    #1
    1. Advertising

  2. 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


    > 1. Will each call get its own stack?


    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

    > 2. How simultaneous calls are processed?


    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
    Ignacio Machin \( .NET/ C# MVP \), Mar 24, 2006
    #2
    1. Advertising

  3. 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

    --
    http://www.openmymind.net/
    http://www.fuelindustries.com/


    "WebMatrix" <> wrote in message
    news:...
    > 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
    > "detrimental
    > 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?
    >
    Karl Seguin [MVP], Mar 24, 2006
    #3
  4. <"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO .
    ANDME net>> wrote:
    > 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).


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

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too
    Jon Skeet [C# MVP], Mar 24, 2006
    #4
  5. I honestly didn't know that. I do know you can do it in vb.net...how
    interesting....



    --
    http://www.openmymind.net/
    http://www.fuelindustries.com/


    "Jon Skeet [C# MVP]" <> wrote in message
    news:...
    > <"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO .
    > ANDME net>> wrote:
    >> 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).

    >
    > You can't declare local variables as static in C# anyway...
    >
    > --
    > Jon Skeet - <>
    > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    > If replying to the group, please do not mail me too
    Karl Seguin [MVP], Mar 24, 2006
    #5
  6. Hi,

    "Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
    net> wrote in message news:%...
    >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).


    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
    Ignacio Machin \( .NET/ C# MVP \), Mar 24, 2006
    #6
  7. =?Utf-8?B?V2ViTWF0cml4?=

    jeff Guest

    jeff, Mar 24, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan Burd

    Thread-Safety in Functions: A Random String Generator

    Jonathan Burd, Jan 6, 2005, in forum: C Programming
    Replies:
    4
    Views:
    475
  2. Replies:
    5
    Views:
    625
    Chris Thomasson
    Aug 23, 2007
  3. Olumide
    Replies:
    6
    Views:
    574
    Ian Collins
    Oct 23, 2007
  4. Hicham Mouline
    Replies:
    5
    Views:
    2,350
    James Kanze
    Dec 19, 2008
  5. Eric Snow

    static statements and thread safety

    Eric Snow, Sep 22, 2011, in forum: Python
    Replies:
    0
    Views:
    164
    Eric Snow
    Sep 22, 2011
Loading...

Share This Page