Ayende @ Rahien

It's a girl

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

Yassir
09/23/2009 08:35 PM by
Yassir

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

Paco
09/23/2009 08:38 PM by
Paco

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.

Tommi
09/23/2009 08:44 PM by
Tommi

It's obvious - with commented: /* static */ it will be 1212 - ThreadStatic attribute works only with static fields.

Dmitry
09/23/2009 09:22 PM by
Dmitry

The attribute will not make an instance field (thread) static, at least I hope it won't.

Brad
09/23/2009 09:46 PM by
Brad

2 2 0 0?

Corey
09/23/2009 10:01 PM by
Corey

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.

Matthew
09/23/2009 10:05 PM by
Matthew

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

configurator
09/23/2009 10:06 PM by
configurator

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.

Thomas Krause
09/24/2009 01:06 AM by
Thomas Krause

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?

Steve Py
09/24/2009 02:07 AM by
Steve Py

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.

Mike Brown
09/24/2009 02:21 AM by
Mike Brown

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.

Mike Brown
09/24/2009 02:26 AM by
Mike Brown

Oops just realized that the variable is not static...hmm, I don't know brain, what will happen here?

Mike Brown
09/24/2009 03:22 AM by
Mike Brown

Ran the code and got the expected 1 2 1 2

Marc Gravell
09/24/2009 06:03 AM by
Marc Gravell

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).

Chris Kemp
09/24/2009 07:38 AM by
Chris Kemp

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? :)

Marc Gravell
09/24/2009 07:55 AM by
Marc Gravell

@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).

Ramon Smits
09/24/2009 12:09 PM by
Ramon Smits

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.

Jim
09/24/2009 12:17 PM by
Jim

Is the output "Hello World"?

Neil Mosafi
10/04/2009 11:53 PM by
Neil Mosafi

Does the program become self-aware and try to take over the world?

Comments have been closed on this topic.