Are static methods thread safe?
Author |
Message |
Chad Myer #1 / 14
|
 Are static methods thread safe?
Quote: > Are static methods thread safe? If I have 2 instances of a class that has a > static method, where is the stack of that static method? when the static > method will be instantiated? Are the parameters of the static method thread > safe?
Nothing is thread-safe unless you make it. If you have a multithreaded app and multiple threads are calling that static method, then you need to design that method to be thread safe. The stack of the method is with the method itself. Every method gets its own stack. It will be allocated in the thread which called the method. "Are the parameters of the static method thread safe?" By parameters you mean arguments? Like the "arg1" in "static void Foo(string arg1)"? Every time you call that method, arg1 only exists in the stack for that method, so if two threads call that method, then each one gets a copy of arg1. Now, if you have an instance of an object that two threads use and both threads call Foo() and pass in the same instance of the object, and Foo() manipulates the object, then you could have problems (race condition). In that case, you should lock the object to prevent problems. Like I said, if you expect threading problems, you should plan to be thread safe. Just by declaring it static or leaving it regular does not make it "thread safe". -c
|
Wed, 13 Apr 2005 00:18:11 GMT |
|
 |
Chris #2 / 14
|
 Are static methods thread safe?
A static method is safe, UNLESS you are manipulating (changing) a static field in that class in your static method. If all the variables come from the parameters and method variables, then it is thread safe. Each thread will have it's own stack for these. Chris R.
Quote: > Are static methods thread safe? If I have 2 instances of a class that has a > static method, where is the stack of that static method? when the static > method will be instantiated? Are the parameters of the static method thread > safe? > Thanks > Chung
|
Wed, 13 Apr 2005 00:28:35 GMT |
|
 |
Willem Kokk #3 / 14
|
 Are static methods thread safe?
Quote: > The stack of the method is with the method itself. Every method gets > its own stack. It will be allocated in the thread which called the > method.
This is wrong.. A method has no stack only a thread has a stack(at least in this context) a method called on a thread will use the stack of that thread. Then, when that thread gets suspended and another thread gets it's timeslice, it has a different stack.. when that static function gets called again, nothing about the previous call is known Willem
|
Wed, 13 Apr 2005 01:06:02 GMT |
|
 |
Willem Kokk #4 / 14
|
 Are static methods thread safe?
Quote: > Are static methods thread safe?
only if you are not referencing data any other thread can reference.. so, if you only pass in value types, and only use local variables in that method, it is thread safe, else you have to synchronise the access to the global data Quote: > If I have 2 instances of a class that has a > static method, where is the stack of that static method?
a method does not have a stack, only threads have stacks.. the stack that that method uses will be the stack of the currently executing thread Quote: > when the static method will be instantiated?
A method is never instantiated, it is just code, it is in the binary you produce by compiling.. if you really wanted to stretch it, you could say that a method is instantiated at compile time (or runtime when you use reflection). Are the parameters of the static method thread data (variables/parameters) has no property of being threadsafe, a function is either threadsafe or not, but you can't say that of an integer.. if a function is threadsafe, the data you pass into it will come out ok.. if you pass the same data to a not threadsafe method, it might come out corrupted. See what i mean?? Writing thread safe code can be a pain in the ass, and i'm not particulary good it, even after reading several books about it, and writing a massive multiplayer server. HTH, Willem
|
Wed, 13 Apr 2005 01:25:24 GMT |
|
 |
CCY #5 / 14
|
 Are static methods thread safe?
Are static methods thread safe? If I have 2 instances of a class that has a static method, where is the stack of that static method? when the static method will be instantiated? Are the parameters of the static method thread safe? Thanks Chung
|
Tue, 12 Apr 2005 23:58:28 GMT |
|
 |
Richard A Low #6 / 14
|
 Are static methods thread safe?
Static methods are NOT inherently thread safe, but will be safe if you pass no parameters, or only pass value types to them and if you use no outside object or static field from within them and generally keep them stateless. In other words, if you synchronize them by stateless, dependancy-less design, rather than by locks. But the stack of each call to a static method will be safe, providing there are no common references among the various calls (as I basically alluded to above). Richard Quote:
> Are static methods thread safe? If I have 2 instances of a class that has a > static method, where is the stack of that static method? when the static > method will be instantiated? Are the parameters of the static method thread > safe? > Thanks > Chung
|
Wed, 13 Apr 2005 00:18:45 GMT |
|
 |
Courageou #7 / 14
|
 Are static methods thread safe?
Quote: >The stack of the method is with the method itself. Every method gets >its own stack. It will be allocated in the thread which called the >method.
Say what? C//
|
Wed, 13 Apr 2005 08:54:36 GMT |
|
 |
Courageou #8 / 14
|
 Are static methods thread safe?
Quote: >Static methods are NOT inherently thread safe, but will be safe if you pass >no parameters, or only pass value types to them and if you use no outside >object or static field from within them and generally keep them stateless. >In other words, if you synchronize them by stateless, dependancy-less >design, rather than by locks.
Parenthetically, when doing multithreaded programming, one should prefer the approaches you just mentioned over the approach of using expansive explicit synchronization, whenever it is straightforward in design to easily do so. Consider the way in which you can easily mark entire methods as "synchronized" in java. This has lead many naive programmers overusing this form, and hence an explosion of programmers that are multithreaded but spend a great deal of time with many blocked threads. Generally, one should "use as much synchronization as necessary, but no more." C//
|
Wed, 13 Apr 2005 08:57:45 GMT |
|
 |
Chad Myer #9 / 14
|
 Are static methods thread safe?
Quote: > >The stack of the method is with the method itself. Every method gets > >its own stack. It will be allocated in the thread which called the > >method. > Say what?
Nevermind. I thought I remember reading somewhere that when a new function is entered, a new stack is created for that method and the arguments pushed onto it. But there's actually just one stack and when the function is called, its parameters are pushed onto the stack and then poped when the function exits. Sorry, CHad
|
Wed, 13 Apr 2005 09:33:25 GMT |
|
 |
Courageou #10 / 14
|
 Are static methods thread safe?
Quote: >> >The stack of the method is with the method itself. Every method gets >> >its own stack. It will be allocated in the thread which called the >> >method. >Nevermind. I thought I remember reading somewhere that when a new >function is entered, a new stack is created for that method and >the arguments pushed onto it. >But there's actually just one stack and when the function is called, >its parameters are pushed onto the stack and then poped when the >function exits.
For what it's worth, there are systems which do what you suggested (well, sort of), and are thereby "stackless". These systems are pretty cool. Recursion is much more manageable in these environments, and you can also build things like coroutines and continuations quite trivially. C//
|
Wed, 13 Apr 2005 11:56:05 GMT |
|
 |
Chungcheng Ye #11 / 14
|
 Are static methods thread safe?
Yes, I agree with you. Originally, I thought that a static method, alike a static field, has only one function stack no matter how many class objects. Except providing "class methods", one can call a static method without create an object, what are other purposes the static methods can provide in C#? Chung
Quote: > > Are static methods thread safe? > only if you are not referencing data any other thread can reference.. > so, if you only pass in value types, and only use local variables in that > method, it is thread safe, else you have to synchronise the access to the > global data > > If I have 2 instances of a class that has a > > static method, where is the stack of that static method? > a method does not have a stack, only threads have stacks.. > the stack that that method uses will be the stack of the currently executing > thread > > when the static method will be instantiated? > A method is never instantiated, it is just code, it is in the binary you > produce by compiling.. > if you really wanted to stretch it, you could say that a method is > instantiated at compile time (or runtime when you use reflection). > Are the parameters of the static method thread > data (variables/parameters) has no property of being threadsafe, a function > is either threadsafe or not, but you can't say that of an integer.. > if a function is threadsafe, the data you pass into it will come out ok.. > if you pass the same data to a not threadsafe method, it might come out > corrupted. > See what i mean?? > Writing thread safe code can be a pain in the ass, and i'm not particulary > good it, even after reading several books about it, and writing a massive > multiplayer server. > HTH, > Willem
|
Wed, 13 Apr 2005 21:04:19 GMT |
|
 |
Willem Kokk #12 / 14
|
 Are static methods thread safe?
unless the 'mess' that is C/C++ static in C# thats about all static does: providing functions and fields that belong to the class instead of a specific instance http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csre... vclrfstaticpg.asp HTH, Willem
Quote: > Yes, I agree with you. Originally, I thought that a static method, alike a > static field, has only one function stack no matter how many class objects. > Except providing "class methods", one can call a static method without > create an object, what are other purposes the static methods can provide in > C#? > Chung
> > > Are static methods thread safe? > > only if you are not referencing data any other thread can reference.. > > so, if you only pass in value types, and only use local variables in that > > method, it is thread safe, else you have to synchronise the access to the > > global data > > > If I have 2 instances of a class that has a > > > static method, where is the stack of that static method? > > a method does not have a stack, only threads have stacks.. > > the stack that that method uses will be the stack of the currently > executing > > thread > > > when the static method will be instantiated? > > A method is never instantiated, it is just code, it is in the binary you > > produce by compiling.. > > if you really wanted to stretch it, you could say that a method is > > instantiated at compile time (or runtime when you use reflection). > > Are the parameters of the static method thread > > data (variables/parameters) has no property of being threadsafe, a > function > > is either threadsafe or not, but you can't say that of an integer.. > > if a function is threadsafe, the data you pass into it will come out ok.. > > if you pass the same data to a not threadsafe method, it might come out > > corrupted. > > See what i mean?? > > Writing thread safe code can be a pain in the ass, and i'm not particulary > > good it, even after reading several books about it, and writing a massive > > multiplayer server. > > HTH, > > Willem
|
Thu, 14 Apr 2005 03:27:12 GMT |
|
 |
Chad Myer #13 / 14
|
 Are static methods thread safe?
Quote:
> > > >The stack of the method is with the method itself. Every method gets > > > >its own stack. It will be allocated in the thread which called the > > > >method. > > > Say what? > > Nevermind. I thought I remember reading somewhere that when a new > > function is entered, a new stack is created for that method and > > the arguments pushed onto it. > A new "stack frame" is created on the current thread's stack for the > arguments and local variables to live in. Perhaps that's what you were > thinking of.
Yes, that's exactly what I was thinking of. Thank you for clearing that up. I didn't think I was going crazy, but I was starting to suspect it. -c
|
Sun, 17 Apr 2005 06:10:20 GMT |
|
 |
Mike Schillin #14 / 14
|
 Are static methods thread safe?
Quote:
> > >The stack of the method is with the method itself. Every method gets > > >its own stack. It will be allocated in the thread which called the > > >method. > > Say what? > Nevermind. I thought I remember reading somewhere that when a new > function is entered, a new stack is created for that method and > the arguments pushed onto it.
A new "stack frame" is created on the current thread's stack for the arguments and local variables to live in. Perhaps that's what you were thinking of.
|
Sun, 17 Apr 2005 06:04:32 GMT |
|
|
|