473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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 "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
6 1226
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
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
<"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
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
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
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.
http://msdn2.microsoft.com/en-us/lib...s3(VS.80).aspx
Mar 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Gareth | last post by:
I want to do the following to strings: 1) Check if first four characters are "DATA" 2) Get the middle 'word' from the following string "DATA 123 xyz" (the middle word is variable length) -...
4
by: The Crow | last post by:
for example i have static readonly SqlParameter and i want to clone them at runtime. as clone operation will not write to SqlParameter object, just reading, should i lock that object during read...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
74
by: Mark | last post by:
Hello Friends Please check the code below. One in C# and other in VB .Net In C# I am not able to access a static property by an instance variable, but in VB I can do it easily. The Error is ...
5
by: WebMatrix | last post by:
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...
4
by: Jimmy | last post by:
I need to use Asynchronous Socket functions in a server application and am learning from sources such as the MSDN2 (http://msdn2.microsoft.com/en-us/library/bbx2eya8.aspx). What I observed is that...
7
by: intrader | last post by:
I have the following small classes: //----------------code--------------- using System; using System.Collections.Generic; using System.Text; namespace ValidatorsLibrary { public class...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
5
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.