A thread static instance?
Without running this code, what would you expect this to do?
public class Strange
{
[ThreadStatic]
public /* static */ int Value;
}
var s1 = new Strange {Value = 1};
var s2 = new Strange { Value = 2 };Console.WriteLine(s1.Value);
Console.WriteLine(s2.Value);ThreadPool.QueueUserWorkItem(state =>
{
Console.WriteLine(s1.Value);
Console.WriteLine(s2.Value);
});
Can you guess?
Comments
I'd expect it to print '0' twice
and '1' and '2' before that...
it will input :
1
2
but if you want it to input them twice you need to keep the appliction running
so if you add Console.Read(); at the end it will print
1
2
1
2
You wouldn't ask it if the answer would not be 1-2-1-2 I guess, but I don't have any idea why.
It's obvious - with commented: /* static */ it will be 1212 - ThreadStatic attribute works only with static fields.
The attribute will not make an instance field (thread) static, at least I hope it won't.
1-2-2-2 !??!?!
2 2 0 0?
In the example it will only print
1
2
Put a sleep or a ReadLine() and it will
1
2
1
2
Because instance variables are not thread static, so you're passing the same references to the threadpool.
The ThreadStaticAttribute only affects static members. So, this would do the same thing it would do if you didn't have the attribute at all:
1
2
1
2
Obviously 1, 2, 1, 2. Like Tommi said, ThreadStatic only affects static fields. If you add the static... Well the code wouldn't compile because you're calling a static field on an instance (s1.Value) but if you fix that you'd get 2, 2, 0, 0.
I hope it prints 1,2,1,2
I would consider everything else a bug (if it is not documented somewhere)....
The attribute should be ignored if not applied to a static field as others explained already, but as you are blogging about it, I guess this is not the actual result of the code, is it?
I'd expect either an error as ThreadStatic is only intended for static members, or it to be ignored, resulting in 1-2-1-2.
However, if ThreadStatic did work on non-static fields (a bug I'd figure) then I'd expect 1-2-2-2.
You'll get
2
2
0
0
Becase it's thread static, the variable is shared among all instances for a given thread. The writelines on the main thread will use the last value assigned (2). The writelines in the threadpool thread are never initialized and have 0.
Oops just realized that the variable is not static...hmm, I don't know brain, what will happen here?
Ran the code and got the expected 1 2 1 2
It behaves like I expected (1,2,1,2) - I guess the main problem is that there simply isn't a way of specifying that an attribute only applies to static (no AttributeTargets enum).
I'd expect it to throw a compiler error, but if it did that then you'd not be posting it on here, would you? :)
@Chris Kemp It shouldn't throw a compiler error unless it is mentioned in the language spec; it isn't mentioned (it is an implementation feature) - and the only other escape-hatch is §17.5 which only applies to attributes under the System.Runtime.InteropServices namespace (ThreadStaticAttribute is in the System namespace).
It would have been very weird if a (thread) static variable could be set via :
new Strange {Value = 1};
As that would seems static at all. So most obviously output must be 1,2, 1,2
Would have been a whole different when the static keyword was not commented as then we could only set it via:
Strange.Value = 1;
And if that was marked with [ThreadStatic] I would expect thread static behavior. Just as others imply.
Is the output "Hello World"?
Does the program become self-aware and try to take over the world?
Comment preview