﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Neil Mosafi commented on A thread static instance?</title><description>Does the program become self-aware and try to take over the world?
</description><link>http://ayende.com/4210/a-thread-static-instance#comment22</link><guid>http://ayende.com/4210/a-thread-static-instance#comment22</guid><pubDate>Sun, 04 Oct 2009 23:53:32 GMT</pubDate></item><item><title>Jim commented on A thread static instance?</title><description>Is the output "Hello World"?
</description><link>http://ayende.com/4210/a-thread-static-instance#comment21</link><guid>http://ayende.com/4210/a-thread-static-instance#comment21</guid><pubDate>Thu, 24 Sep 2009 12:17:52 GMT</pubDate></item><item><title>Ramon Smits commented on A thread static instance?</title><description>  
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.
</description><link>http://ayende.com/4210/a-thread-static-instance#comment20</link><guid>http://ayende.com/4210/a-thread-static-instance#comment20</guid><pubDate>Thu, 24 Sep 2009 12:09:33 GMT</pubDate></item><item><title>Marc Gravell commented on A thread static instance?</title><description>@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).
</description><link>http://ayende.com/4210/a-thread-static-instance#comment19</link><guid>http://ayende.com/4210/a-thread-static-instance#comment19</guid><pubDate>Thu, 24 Sep 2009 07:55:17 GMT</pubDate></item><item><title>Chris Kemp commented on A thread static instance?</title><description>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? :)
</description><link>http://ayende.com/4210/a-thread-static-instance#comment18</link><guid>http://ayende.com/4210/a-thread-static-instance#comment18</guid><pubDate>Thu, 24 Sep 2009 07:38:17 GMT</pubDate></item><item><title>Marc Gravell commented on A thread static instance?</title><description>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).
</description><link>http://ayende.com/4210/a-thread-static-instance#comment17</link><guid>http://ayende.com/4210/a-thread-static-instance#comment17</guid><pubDate>Thu, 24 Sep 2009 06:03:23 GMT</pubDate></item><item><title>Mike Brown commented on A thread static instance?</title><description>Ran the code and got the expected 1 2 1 2
</description><link>http://ayende.com/4210/a-thread-static-instance#comment16</link><guid>http://ayende.com/4210/a-thread-static-instance#comment16</guid><pubDate>Thu, 24 Sep 2009 03:22:29 GMT</pubDate></item><item><title>Mike Brown commented on A thread static instance?</title><description>Oops just realized that the variable is not static...hmm, I don't know brain, what will happen here?
</description><link>http://ayende.com/4210/a-thread-static-instance#comment15</link><guid>http://ayende.com/4210/a-thread-static-instance#comment15</guid><pubDate>Thu, 24 Sep 2009 02:26:05 GMT</pubDate></item><item><title>Mike Brown commented on A thread static instance?</title><description>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.
</description><link>http://ayende.com/4210/a-thread-static-instance#comment14</link><guid>http://ayende.com/4210/a-thread-static-instance#comment14</guid><pubDate>Thu, 24 Sep 2009 02:21:18 GMT</pubDate></item><item><title>Steve Py commented on A thread static instance?</title><description>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.
</description><link>http://ayende.com/4210/a-thread-static-instance#comment13</link><guid>http://ayende.com/4210/a-thread-static-instance#comment13</guid><pubDate>Thu, 24 Sep 2009 02:07:53 GMT</pubDate></item><item><title>Thomas Krause commented on A thread static instance?</title><description>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?
</description><link>http://ayende.com/4210/a-thread-static-instance#comment12</link><guid>http://ayende.com/4210/a-thread-static-instance#comment12</guid><pubDate>Thu, 24 Sep 2009 01:06:53 GMT</pubDate></item><item><title>configurator commented on A thread static instance?</title><description>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.
</description><link>http://ayende.com/4210/a-thread-static-instance#comment11</link><guid>http://ayende.com/4210/a-thread-static-instance#comment11</guid><pubDate>Wed, 23 Sep 2009 22:06:03 GMT</pubDate></item><item><title>Matthew commented on A thread static instance?</title><description>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
  
  
</description><link>http://ayende.com/4210/a-thread-static-instance#comment10</link><guid>http://ayende.com/4210/a-thread-static-instance#comment10</guid><pubDate>Wed, 23 Sep 2009 22:05:15 GMT</pubDate></item><item><title>Corey commented on A thread static instance?</title><description>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.
</description><link>http://ayende.com/4210/a-thread-static-instance#comment9</link><guid>http://ayende.com/4210/a-thread-static-instance#comment9</guid><pubDate>Wed, 23 Sep 2009 22:01:05 GMT</pubDate></item><item><title>Brad commented on A thread static instance?</title><description>2 2 0 0?
</description><link>http://ayende.com/4210/a-thread-static-instance#comment8</link><guid>http://ayende.com/4210/a-thread-static-instance#comment8</guid><pubDate>Wed, 23 Sep 2009 21:46:30 GMT</pubDate></item><item><title>Marco Amendola commented on A thread static instance?</title><description>1-2-2-2 !??!?!
</description><link>http://ayende.com/4210/a-thread-static-instance#comment7</link><guid>http://ayende.com/4210/a-thread-static-instance#comment7</guid><pubDate>Wed, 23 Sep 2009 21:32:24 GMT</pubDate></item><item><title>Dmitry commented on A thread static instance?</title><description>The attribute will not make an instance field (thread) static, at least I hope it won't.
</description><link>http://ayende.com/4210/a-thread-static-instance#comment6</link><guid>http://ayende.com/4210/a-thread-static-instance#comment6</guid><pubDate>Wed, 23 Sep 2009 21:22:56 GMT</pubDate></item><item><title>Tommi commented on A thread static instance?</title><description>It's obvious - with commented: /* static */ it will be 1212 - ThreadStatic attribute works only with static fields.
</description><link>http://ayende.com/4210/a-thread-static-instance#comment5</link><guid>http://ayende.com/4210/a-thread-static-instance#comment5</guid><pubDate>Wed, 23 Sep 2009 20:44:33 GMT</pubDate></item><item><title>Paco commented on A thread static instance?</title><description>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.
</description><link>http://ayende.com/4210/a-thread-static-instance#comment4</link><guid>http://ayende.com/4210/a-thread-static-instance#comment4</guid><pubDate>Wed, 23 Sep 2009 20:38:55 GMT</pubDate></item><item><title>Yassir commented on A thread static instance?</title><description>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
</description><link>http://ayende.com/4210/a-thread-static-instance#comment3</link><guid>http://ayende.com/4210/a-thread-static-instance#comment3</guid><pubDate>Wed, 23 Sep 2009 20:35:33 GMT</pubDate></item><item><title>Krzysztof Kozmic commented on A thread static instance?</title><description>and  '1' and '2' before that...
  
</description><link>http://ayende.com/4210/a-thread-static-instance#comment2</link><guid>http://ayende.com/4210/a-thread-static-instance#comment2</guid><pubDate>Wed, 23 Sep 2009 20:21:02 GMT</pubDate></item><item><title>Krzysztof Kozmic commented on A thread static instance?</title><description>I'd expect it to print '0' twice
</description><link>http://ayende.com/4210/a-thread-static-instance#comment1</link><guid>http://ayende.com/4210/a-thread-static-instance#comment1</guid><pubDate>Wed, 23 Sep 2009 20:19:29 GMT</pubDate></item></channel></rss>