views:

72

answers:

4

C# Question - I'm trying to determine whether it is OK to use a static method where, within the method it does have some local variables it uses. Are the local variables "shared" across usages of the method? What happens for example if the static method is being called/used at the same time from different threads? Does one thread block until the other is complete etc?

Perhaps the generalised question is, in a threaded application, when should one "not" being using a static method?

+1  A: 

No method of any kind in C# shares local variables.

You may be thinking of Static functions in VB.

John Saunders
...except for closures
Tergiver
+1  A: 

There are plenty of reasons to use static methods in multi-threaded apps. Nothing wrong with that either. As long as you do not change any global variables (without locking) you should have no problems there.

liho1eye
Downvoted because this answer doesn’t address most of the question; and because the term “global variables” is misleading. (You may have meant fields, but they are neither global, nor are they variables.)
Timwi
that's pedantry :(
seand
-1: anyone who insists on using the term "global variable" in C# without any explanation even after pointed out is a no-hire.
romkyns
well it is good then I am happily employed by people who know that anything that can be modified is a variable and have at least basic comprehension of scope.
liho1eye
+6  A: 

Local variables within the method live on the stack and each thread has its own stack. Therefore it's safe for multiple threads to use the method.

However if the method itself uses static variables then you should use appropriate MT protection. Also external methods you may be calling need to be safe...

seand
Downvoted mostly because this answer doesn’t address all of the question, but also because of the comment about “static variables”. There is no such thing. If you meant “static fields”, then it is irrelevant whether they are static.
Timwi
@Timwi you are arguing semantics. To me it is perfectly clear what he meant.
liho1eye
@liho1eye: I’m sure it is, it was clear to me too, but it nonetheless doesn’t answer all of the question.
Timwi
@Timwi right, so basically you are downvoting people to get your answer to the top.
liho1eye
@liho1eye: No, I’m downvoting answers that are incomplete or wrong. What about you? Did you just downvote my answer just because you didn’t like my comment here? Or is there something actually wrong with my answer (in which case I would be delighted to see a comment explaining it)?
Timwi
@Timwi There is nothing wrong with his answer nor mine. They are not chewing out details, but that hardly qualifies as "incomplete". And yes I downvoted your answer because I don't like your attitude. Sorry if I cannot be bothered to try to find spelling mistakes in your answer to justify it.
liho1eye
@liho1eye: Please elaborate — what is it about my “attitude”? It is my belief that downvoting incorrect or incomplete answers is the recommended/expected attitude on SO, while downvoting for reasons of personal liking is not.
Timwi
@Timwi - fields **ARE** variables (unless marked const or readonly). By definition - they vary.
Mark H
+1 comp vote. This answer is correct and complete. C# language spec, chapter 5.
Hans Passant
A: 

Are the local variables "shared" across usages of the method?

No, they are not. Each thread executing the method has its own copy of the local variables, and they are independent of each other. When the method returns, the particular copy of locals for that particular thread is discarded. (*)

What happens for example if the static method is being called/used at the same time from different threads? Does one thread block until the other is complete etc?

No, they don’t; they will just execute two copies of the method at the same time. If you actually want them to block, use the lock statement, which causes the second thread that enters the lock statement to wait until the first one returns from the lock statement. This may be necessary if your method accesses (non-local) fields, which are shared data.

In a threaded application, when should one not use a static method?

Whether you should use a static method or not depends on whether the method requires an object to operate on, but has nothing to do with whether your application is multi-threaded or not. For the purpose of threading, a static method is nothing special compared to a non-static method.

(*) This may no longer be true if you have a lambda expression or anonymous method inside the method that uses the local variables, but this is a technicality.

Timwi
Downvoting another answer in a thread you posted to is considered a serious breach of etiquette by many users here. Well, you found out.
Hans Passant
@Hans: WTF? If you think it's a breach then **go on meta and ask it to be forbidden**! (and I'll support that request). But until then it's allowed and YOU are in breach of etiquette by downvoting out of spite.
romkyns
@romkyns: please don't jump to conclusions, I didn't do any downvoting here.
Hans Passant