Static Functions and Thread Safety. How does it work?

G

Guest

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?
 
I

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

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)
 
K

Karl Seguin [MVP]

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
 
J

Jon Skeet [C# MVP]

<"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO .
ANDME said:
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...
 
K

Karl Seguin [MVP]

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

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

Hi,

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top