Reputation: 15928
When you create an instance of a class, all the variables in that instance is specific to that instance and get killed when the instance is out of scope. But how does it work in a static method? Suppose two people call System.Math.Abs() at exactly the same time. How does the runtime differentiate between the two callers? Is this where threading comes in? Are separate threads automatically created for each caller?
Upvotes: 9
Views: 2122
Reputation: 659994
When you create an instance of a class, all the variables specific to that instance are killed when the instance is out of scope.
The variables -- usually called "fields" are deallocated after the lifetime of the instance. Scope is the region of program text in which the compiler recognizes something by its name; liftime is the portion of time during which a storage location is valid. Scope and lifetime are often confused.
But how does it work in a static method?
Static fields have unbounded lifetimes; the storage location is created at some time before the field is accessed and not destroyed until the appdomain is torn down.
Suppose two people call System.Math.Abs() at exactly the same time.
OK. How do you propose that happens?
How does the runtime differentiate between the two callers? Is this where threading comes in?
The static method is jitted into a bunch of machine instructions that are numbers in memory. Each thread of execution has a number associated with it called the instruction pointer which locates the current instruction. Two different threads can both have instruction pointers that are inside the same static method at the same time.
Are separate threads automatically created for each caller?
The question doesn't make any sense. How did you get two callers at the same time if they weren't already on separate threads?
Upvotes: 12
Reputation: 38397
All static members of a class exist even if no instances of the class exist. They get initialized sometime before first use and get cleaned up when the program is complete.
If you had two simultaneous calls to a static method, they would be working with the same copy of any static members of the class (if they use them). So if the static method operates on a static member (or parameter), it should do so in a thread safe way. If the static method only operates on locals then the method itself is typically thread safe.
As for how does the runtime differentiate between two callers, this is the essence of threading. Each thread has its own call stack with its own copy of any local variables, parameters, return address, etc. So the two calls do not get confused and each return to their caller correctly. The only concern, again, is if the static method operates on a static member that isn't thread safe (or a parameter that isn't thread safe).
Upvotes: 6
Reputation: 1062600
There is no real difference between static and non-static methods in terms of method-variable lifetime. In both cases, as an implementation detail, locals are typically (not quite always: there are exceptions) allocated on the stack. The stack is per-thread, so local method variables do not crossover between threads.
The only difference here between instance and static is that instance methods have an implicit zeroth parameter, aka "this", that is pushed by the caller (plus some virtual dispatch and null-check fun).
For simplicity, I'm glossing over iterator blocks, captured variables, etc.
Upvotes: 10