By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,728 Members | 753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,728 IT Pros & Developers. It's quick & easy.

Static Functions and Thread Safety. How does it work?

P: n/a
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?

Mar 24 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Mar 24 '06 #2

P: n/a
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" <We*******@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
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?

Mar 24 '06 #3

P: n/a
<"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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 24 '06 #4

P: n/a
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 24 '06 #5

P: n/a
Hi,

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
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
Mar 24 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.