Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,125 | Comments: 45,488

filter by tags archive

A thread static instance?

time to read 1 min | 104 words

Without running this code, what would you expect this to do?

public class Strange
public /* static */ int Value;

var s1 = new Strange {Value = 1};
var s2 = new Strange { Value = 2 };
ThreadPool.QueueUserWorkItem(state =>

Can you guess?



it will input :



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






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.


2 2 0 0?


In the example it will only print



Put a sleep or a ReadLine() and it will





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:






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

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

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

You'll get





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

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

Mike Brown

Ran the code and got the expected 1 2 1 2

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

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

@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

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"?

Neil Mosafi

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

Comment preview

Comments have been closed on this topic.


  1. RavenDB 3.5 Whirlwind tour: I need to be free to explore my data - one day from now
  2. RavenDB 3.5 whirl wind tour: I'll have the 3+1 goodies to go, please - 4 days from now
  3. The design of RavenDB 4.0: Voron has a one track mind - 5 days from now
  4. RavenDB 3.5 whirl wind tour: Digging deep into the internals - 6 days from now
  5. The design of RavenDB 4.0: Separation of indexes and documents - 7 days from now

And 11 more posts are pending...

There are posts all the way to May 30, 2016


  1. The design of RavenDB 4.0 (14):
    05 May 2016 - Physically segregating collections
  2. RavenDB 3.5 whirl wind tour (14):
    04 May 2016 - I’ll find who is taking my I/O bandwidth and they SHALL pay
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats