Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ThreadLocal(T) Class
Collapse the table of content
Expand the table of content

ThreadLocal<T> Class

.NET Framework 4.6 and 4.5

Provides thread-local storage of data.

System.Object
  System.Threading.ThreadLocal<T>

Namespace:  System.Threading
Assemblies:   System.Threading (in System.Threading.dll)
  mscorlib (in mscorlib.dll)

public class ThreadLocal<T> : IDisposable

Type Parameters

T

Specifies the type of data stored per-thread.

The ThreadLocal<T> type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibraryThreadLocal<T>()Initializes the ThreadLocal<T> instance.
Public methodThreadLocal<T>(Boolean)Initializes the ThreadLocal<T> instance.
Public methodSupported by Portable Class LibraryThreadLocal<T>(Func<T>)Initializes the ThreadLocal<T> instance with the specified valueFactory function.
Public methodThreadLocal<T>(Func<T>, Boolean)Initializes the ThreadLocal<T> instance with the specified valueFactory function.
Top

  NameDescription
Public propertySupported by Portable Class LibraryIsValueCreatedGets whether Value is initialized on the current thread.
Public propertySupported by Portable Class LibraryValueGets or sets the value of this instance for the current thread.
Public propertyValuesGets a list for all of the values currently stored by all of the threads that have accessed this instance.
Top

  NameDescription
Public methodSupported by Portable Class LibraryDispose()Releases all resources used by the current instance of the ThreadLocal<T> class.
Protected methodSupported by Portable Class LibraryDispose(Boolean)Releases the resources used by this ThreadLocal<T> instance.
Public methodSupported by Portable Class LibraryEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryFinalizeReleases the resources used by this ThreadLocal<T> instance. (Overrides Object.Finalize().)
Public methodSupported by Portable Class LibraryGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Portable Class LibraryToStringCreates and returns a string representation of this instance for the current thread. (Overrides Object.ToString().)
Top

The following example shows how to use ThreadLocal<T>:

using System;
using System.Threading;
using System.Threading.Tasks;

class ThreadLocalDemo
{

        // Demonstrates: 
        //      ThreadLocal(T) constructor 
        //      ThreadLocal(T).Value 
        //      One usage of ThreadLocal(T) 
        static void Main()
        {
            // Thread-Local variable that yields a name for a thread
            ThreadLocal<string> ThreadName = new ThreadLocal<string>(() =>
            {
                return "Thread" + Thread.CurrentThread.ManagedThreadId;
            });

            // Action that prints out ThreadName for the current thread
            Action action = () =>
            {
                // If ThreadName.IsValueCreated is true, it means that we are not the 
                // first action to run on this thread. 
                bool repeat = ThreadName.IsValueCreated;

                Console.WriteLine("ThreadName = {0} {1}", ThreadName.Value, repeat ? "(repeat)" : "");
            };

            // Launch eight of them.  On 4 cores or less, you should see some repeat ThreadNames
            Parallel.Invoke(action, action, action, action, action, action, action, action);

            // Dispose when you are done
            ThreadName.Dispose();
        }
}

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library

With the exception of Dispose, all public and protected members of ThreadLocal<T> thread-safe and may be used concurrently from multiple threads. The value returned for the Value and IsValueCreated properties is specific for the thread on which the property is accessed.

Show:
© 2015 Microsoft