Ayende @ Rahien

Hi!
My name is Ayende Rahien
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

@

Posts: 5,947 | Comments: 44,540

filter by tags archive

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

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

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

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

Dmitry

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

Brad

2 2 0 0?

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

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

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

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

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.

Jim
Jim

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.

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. RavenDB Sharding (2):
    21 May 2015 - Adding a new shard to an existing cluster, the easy way
  2. The RavenDB Comic Strip (2):
    20 May 2015 - Part II – a team in trouble!
  3. Challenge (45):
    28 Apr 2015 - What is the meaning of this change?
  4. Interview question (2):
    30 Mar 2015 - fix the index
  5. Excerpts from the RavenDB Performance team report (20):
    20 Feb 2015 - Optimizing Compare – The circle of life (a post-mortem)
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats