1

If you set the ThreadStatic flag on a static field, each thread that runs would have a separate variable, so if you have a static int field and a method that just increments its value 5 times starting that method on two separate threads will just give you two separate ints with a value of 5, instead of one with a value of 10.

In that case, what is the difference between this approach and having a non-static field that gets instantiated for every thread?

6
  • Your function is not nessesarily going to be run by a Thread. Making asumptions that you Function will always/never be called from Multiple threads is a bad idea. This way, it can adapt to the realities. Commented Apr 19, 2018 at 21:24
  • @Christopher: Everything executes on a thread. It's not clear what you mean by "Your function is not nessesarily going to be run by a Thread" Commented Apr 19, 2018 at 21:25
  • @DaisyShipton: Unless you are the one creating the Thread, how can you be sure the thread has a local copy? Commented Apr 19, 2018 at 21:27
  • @Christopher: What do you mean by "a local copy"? The point of [ThreadStatic] is that it effectively has an independent variable per thread. I'm really unclear on what you're trying to say here. Commented Apr 19, 2018 at 21:28
  • @DaisyShipton: To my understanding the question was about "Thread Static" vs "having a seperate variable for each Thread". That is what I answered too. Commented Apr 19, 2018 at 23:07

1 Answer 1

2

ThreadStaticAttribute is decorated with:

[AttributeUsageAttribute(AttributeTargets.Field, Inherited = false)] 

So you can't apply it to methods, only fields. Each thread effectively has an independent variable corresponding to that field. But it can only be applied to static fields - you can't have "per instance and per thread" fields, which is what you'd effectively be asking for.

Now yes, if you're in sufficient control of the threads that are running the code that you're able to create a separate object for each thread, and use an instance field instead, then yes, that's a perfectly fine alternative to ThreadStatic. However, that's not always the situation you're in. Sometimes you need to write code which is safe to call from multiple threads, but isn't nicely partitioned into a separate object per thread. ThreadStatic is useful for that.

Having said that, I'd generally use ThreadLocal<T> instead of ThreadStatic as an alternative approach to having per-thread data.

As a side note, you can't have a static variable within a method. You can only declare local variables within methods, and they're neither static fields nor instance fields - they're just local variables. You can have a static field that happens to only be used within a single method, but that's not the same as declaring the variable within the method.

Sign up to request clarification or add additional context in comments.

7 Comments

Oh, yes, my mistake, I worded it badly. I will try and edit my question.
As a general rule, you should always avoid the static approach. A instance assigned to a static variable is about the closest I ever got to it. On the list of "things to avoid" static is somewhere around a naked goto. As with that one, you can not always do so. But if you can not avoid it, it is usually because someone else did not avoid it when he could.
@Christopher why, though? I've used static methods and variables in a simple application where I always want to have only one instance of them, it makes it so much easier and it NOT being static in that case would just add unneeded complexity.
@J.Doe: Static state is global state. That makes it much harder to reason about, and the problem gets worse as the program gets larger. It also makes it harder to test.
@J.Doe: Anyone that ever had to replace something like the Windows SettingsManager (msdn.microsoft.com/en-us/library/…) with a non-static one or a Databse Access class with a new one (because the Backend DB Changed) can explain you en detail why a static approach are a PITA. Turning static to non-static or replacing one static classe with another requres changing literally every use in the code. If you went the isntance route, it is changing the initialsiation step. Wich can even be done at runtime.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.