This topic has not yet been rated - Rate this topic

ThreadLocal<T> Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Provides thread-local storage of data.

System.Object
  System.Threading.ThreadLocal<T>

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
	ExternalThreading = true)]
public class ThreadLocal<T> : IDisposable

Type Parameters

T

Specifies the type of data stored per-thread.

The ThreadLocal<T> type exposes the following members.

  Name Description
Public method ThreadLocal<T>() Initializes the ThreadLocal<T> instance.
Public method ThreadLocal<T>(Boolean) Initializes the ThreadLocal<T> instance.
Public method ThreadLocal<T>(Func<T>) Initializes the ThreadLocal<T> instance with the specified valueFactory function.
Public method ThreadLocal<T>(Func<T>, Boolean) Initializes the ThreadLocal<T> instance with the specified valueFactory function.
Top
  Name Description
Public property IsValueCreated Gets whether Value is initialized on the current thread.
Public property Value Gets or sets the value of this instance for the current thread.
Public property Values Gets a list for all of the values currently stored by all of the threads that have accessed this instance.
Top
  Name Description
Public method Dispose() Releases all resources used by the current instance of the ThreadLocal<T> class.
Protected method Dispose(Boolean) Releases the resources used by this System.Threading.ThreadLocal{T} instance.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Releases the resources used by this System.Threading.ThreadLocal{T} instance. (Overrides Object.Finalize().)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Creates and returns a string representation of this instance for the current thread. (Overrides Object.ToString().)
Top
Note Note

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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.5, 4

.NET Framework Client Profile

Supported in: 4

Windows 8 Consumer Preview, Windows Server 8 Beta, Windows 7, Windows Server 2008 SP2, Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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.

Did you find this helpful?
(1500 characters remaining)