Thread- Local Storage of Data in .NET


Suppose you're writing a multi-threaded application and you want each thread to have its own copy of some data. You also want this data to persist throughout the lifetime of the thread.
As each thread has its own stack, it also has its own copy of any local variables. However, the trouble with these is that they are not persistent - once the current method has finished executing they're destroyed.
Also, local variables are scoped to the current method and so, if you want them to be available to other methods which the current method calls, then you need to pass them as parameters. This can get rather messy if there are several such variables and multiple method calls.
You could use a static field to store persistent data but then there's only one copy of the data which all threads share.
So is there anything you can use so that each thread has its own copy of persistent data?
The answer is Thread-Local Storage or TLS for short.
There are three ways to create TLS using a .NET application each of which is discussed in turn below.

The ThreadStatic attribute

If you decorate a static field with the ThreadStatic attribute, then each thread will have its own copy of that field. Here's an example:
  1. using System;  
  2. using System.Threading;  
  4. class ThreadStaticTest  
  5. {  
  6.     [ThreadStatic]  
  7.     static string greeting;  
  9.     static void Main()  
  10.     {  
  11.         greeting = "Goodbye from the main thread";  
  12.         Thread t = new Thread(ThreadMethod);  
  13.         t.Start();  
  14.         t.Join();  
  15.         Console.WriteLine(greeting); // prints the main thread's copy  
  16.         Console.ReadKey();  
  17.     }  
  19.     static void ThreadMethod()  
  20.     {  
  21.         greeting = "Hello from the second thread"// only affects the second thread's copy  
  22.         Console.WriteLine(greeting);  
  23.     }  
The output is:
However, the ThreadStatic attribute has a couple of shortcomings:
  • It doesn't work with instance fields.
  • If you initialize the static field to some non-default value (either where it's defined or using a static constructor), then it gets initialized only once for the thread which is running at the time. Other threads only get its default value.
The effect of the second problem can be seen by running this modified version of the above program:
  1. class ThreadStaticTest2  
  2. {  
  3.     [ThreadStatic]  
  4.     static string greeting = "Greetings from the current thread";  
  6.     static void Main()  
  7.     {  
  8.         Console.WriteLine(greeting); // prints initial value  
  9.         greeting = "Goodbye from the main thread";  
  10.         Thread t = new Thread(ThreadMethod);  
  11.         t.Start();  
  12.         t.Join();  
  13.         Console.WriteLine(greeting); // prints the main thread's copy  
  14.         Console.ReadKey();  
  15.     }  
  17.     static void ThreadMethod()  
  18.     {  
  19.         Console.WriteLine(greeting); // prints nothing as greeting initialized on main thread  
  20.         greeting = "Hello from the second thread"// only affects the second thread's copy  
  21.         Console.WriteLine(greeting);  
  22.     }  
  23. }        
The output is now:
Notice that the second line is blank because the second thread's copy of 'greeting' is null, initially.

The ThreadLocal<T> class

This is a class that was introduced in .NET 4.0 and which solves the problems with the ThreadStatic attribute.
Here's a similar program but this time using ThreadLocal<T> for both an instance and a static field and providing them with initial non-default values:
  1. using System;  
  2. using System.Threading;  
  4. class ThreadLocalTest  
  5. {  
  6.     static ThreadLocal<string> greeting;  
  7.     ThreadLocal<int> numThreads = new ThreadLocal<int>(() => 1); // instance field  
  9.     static ThreadLocalTest()  
  10.     {  
  11.         greeting = new ThreadLocal<string>(() => "Greetings from the current thread");  
  12.     }  
  14.     static void Main()  
  15.     {  
  16.         Console.WriteLine(greeting.Value.Replace("current""main"));  
  17.         greeting.Value = "Goodbye from the main thread";  
  18.         Thread t = new Thread(ThreadMethod);  
  19.         t.Start();  
  20.         t.Join();  
  21.         ThreadLocalTest tl = new ThreadLocalTest();  
  22.         Thread t2 = new Thread(tl.InstanceThreadMethod);  
  23.         t2.Start();  
  24.         t2.Join();  
  25.         Console.WriteLine("The number of current threads is now " + tl.numThreads);  
  26.         Console.WriteLine(greeting.Value); // prints the main thread's copy which is still 1  
  27.         Console.ReadKey();  
  28.     }  
  30.     static void ThreadMethod()  
  31.     {  
  32.         Console.WriteLine(greeting.Value.Replace("current""second"));  
  33.         greeting.Value = "Hello from the second thread"// only affects the second thread's copy  
  34.         Console.WriteLine(greeting.Value);  
  35.     }  
  37.     void InstanceThreadMethod()  
  38.     {  
  39.         numThreads.Value++; // increment this thread's copy to 2  
  40.         Console.WriteLine("The number of current threads is " + this.numThreads);  
  41.     }  
The output is:
Notice that the ThreadLocal<T> constructor takes a Func<T> argument i.e. a delegate for a method with no parameters which returns a value of type T. Here we're passing it a compatible lambda expression.
Also, the thread-local variable is initialized 'lazily' i.e. it's not initialized until a thread first attempts to retrieve its Value property. You can check whether it's been initialized yet for the current thread with the IsValueCreated property.

Thread.SetData and Thread.GetData

These two static methods store and retrieve data for each thread in a local data store 'slot'. If you give this slot a name then it can be accessed throughout the application using that name, no matter where it was originally allocated.
This doesn't apply to unnamed slots which are only accessible if allocated within the current scope or exposed via a globally accessible variable.
Each thread in the application can store its own copy of the data within a single named or unnamed slot.
Here's a version of the earlier programs which uses a named slot:
  1. using System;  
  2. using System.Threading;  
  4. class ThreadNamedSlotTest  
  5. {  
  6.     static LocalDataStoreSlot greeting = Thread.AllocateNamedDataSlot("greeting");  
  8.     static void Main()  
  9.     {  
  10.         Thread.SetData(greeting, "Goodbye from the main thread");  
  11.         Thread t = new Thread(ThreadMethod);  
  12.         t.Start();  
  13.         t.Join();  
  14.         Console.WriteLine(Thread.GetData(greeting)); // prints the main thread's copy  
  15.         Thread.FreeNamedDataSlot("greeting"); // releases across all threads  
  16.         Console.ReadKey();  
  17.     }  
  19.     static void ThreadMethod()  
  20.     {  
  21.         Thread.SetData(greeting, "Hello from the second thread"); // only affects the second thread's copy  
  22.         Console.WriteLine(Thread.GetData(greeting));  
  23.         OtherClass oc = new OtherClass();  
  24.         oc.Print();  
  25.     }  
  26. }  
  28. class OtherClass  
  29. {  
  30.     public void Print()  
  31.     {  
  32.         Console.WriteLine("The next line is printed from other class ...");  
  33.         LocalDataStoreSlot greeting = Thread.GetNamedDataSlot("greeting");  
  34.         Console.WriteLine(Thread.GetData(greeting));  
  35.     }  
The output is:
To use an unnamed slot, the declaration of the 'greeting' field would need to be changed to:
  1. static LocalDataStoreSlot greeting = Thread.AllocateDataSlot(); 
However, unless you made this field public, it would not then be accessible from OtherClass.
Using data slots has a couple of drawbacks:
  • They're slower than the other two TLS approaches.
  • The data is not strongly typed. Both the SetData and GetData methods take parameters of type Object.


Prior to the advent of .NET 4.0, thread-local storage was a little known (and understood) aspect of .NET's threading support even though the underlying mechanism is provided by the operating system itself.
However, it has assumed greater importance with the introduction of parallel programming support and the ThreadLocal<T> class in .NET 4.0. This is because it enables each thread to access its own copy of an object rather than a single shared object and thereby achieve thread-safety without the need for locks.
The examples used in this article are also available in the accompanying download for anyone who would like to play around with the code.