I have just hit something weird when I was exploring Threads and Tasks using ThreadStatic Attribute. I believe this may be very specific to Threads and Tasks. Consider the below code snippet:
[ThreadStatic]
static int range=10;
Action action = () =>
{Console.WriteLine("Thread = {0}, Value = {1}", Thread.CurrentThread.ManagedThreadId, range);
Parallel.Invoke( action, action);
This gives the output:
Thread = 2, Value = 10
Thread = 3, Value = 0
This is absolutely fine as ThreadStatic variables can only be initialized once and so, the second line shows it as 0.
But, consider the below scenario:
[ThreadStatic]
static int range=10;
new Thread(() =>
{
Console.WriteLine("Thread = {0}, Value = {1}" Thread.CurrentThread.ManagedThreadId, range);
}).Start();
new Thread(() =>
{
Console.WriteLine("Thread = {0}, Value = {1}" Thread.CurrentThread.ManagedThreadId, range);
}).Start();
This line gives me the output:
Thread = 6, Value = 0
Thread = 7, Value = 0
How much ever Thread I span, I couldn't really see the 'range' value being initialized and displayed as 10. Where is the range variable being initialized here and why there is discrimination between Threads and Tasks in initializing the static variables?
Am I missing something basic here ? Thanks in advance.