473,761 Members | 5,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

Mar 24 '06 #1
5 7132
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*******@disc ussions.microso ft.com> wrote in message
news:F1******** *************** ***********@mic rosoft.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
"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?

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.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
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<"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.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

Mar 24 '06 #5
Hi,

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:%2******** ********@TK2MSF TNGP11.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
9916
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) - extract the "123". 3) Get the last word/number from the following string "DATA SEND 1467436267" What functions should I use to achieve this? I'm new to C++ and finding
11
2255
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 multithreaded operations. Any instance members are not guaranteed to be thread safe." I have 2 questions: 1. I thought dynamic variables are thread-safe since threads have their own
74
5086
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 Static member 'ClassInCS.ABC.MyInt' cannot be accessed with an instance reference; qualify it with a type name
6
1239
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 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...
4
5903
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 all callback handlers in examples are static functions. I did try non-static callback handlers; they certainly works, and they usually make my code simpler. For example, a typical tutorial will start an asynchronous connection with:
7
2003
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 ValidatorBase {
55
6244
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 C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
6
3883
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 variables, just static members static void FirstFunction( args & ); static void SecondFunction( args & ); static void ThirdFunction( args & );
5
1737
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 members Y is the location of a file to be read. The original design assumes that this class will be instantiated and each instance will happily mange its own members. (ie One file location per instance...no thread-safety). Now another class A...
0
9538
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9353
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9909
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7342
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.