Allocates a named data slot on all threads. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.
Assembly: mscorlib (in mscorlib.dll)
<HostProtectionAttribute(SecurityAction.LinkDemand, SharedState := True, ExternalThreading := True)> _ Public Shared Function AllocateNamedDataSlot ( _ name As String _ ) As LocalDataStoreSlot
[HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true, ExternalThreading = true)] public static LocalDataStoreSlot AllocateNamedDataSlot( string name )
[HostProtectionAttribute(SecurityAction::LinkDemand, SharedState = true, ExternalThreading = true)] public: static LocalDataStoreSlot^ AllocateNamedDataSlot( String^ name )
[<HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true, ExternalThreading = true)>] static member AllocateNamedDataSlot : name:string -> LocalDataStoreSlot
Parameters
- name
- Type: System.String
The name of the data slot to be allocated.
| Exception | Condition |
|---|---|
| ArgumentException |
A named data slot with the specified name already exists. |
Important
|
|---|
|
The .NET Framework provides two mechanisms for using thread local storage (TLS): thread-relative static fields (that is, fields that are marked with the ThreadStaticAttribute attribute) and data slots. Thread-relative static fields provide much better performance than data slots, and enable compile-time type checking. For more information about using TLS, see Thread Local Storage: Thread-Relative Static Fields and Data Slots. |
Threads use a local store memory mechanism to store thread-specific data. The common language runtime allocates a multi-slot data store array to each process when it is created. The thread can allocate a data slot in the data store, store and retrieve a data value in the slot, and free the slot for reuse after the thread expires. Data slots are unique per thread. No other thread (not even a child thread) can get that data.
It is not necessary to use the AllocateNamedDataSlot method to allocate a named data slot, because the GetNamedDataSlot method allocates the slot if it has not already been allocated.
Note
|
|---|
|
If the AllocateNamedDataSlot method is used, it should be called in the main thread at program startup, because it throws an exception if a slot with the specified name has already been allocated. There is no way to test whether a slot has already been allocated. |
Slots allocated with this method must be freed with FreeNamedDataSlot.
Note
|
|---|
|
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: SharedState | 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. |
This section contains two code examples. The first example shows how to use a field that is marked with the ThreadStaticAttribute attribute to hold thread-specific information. The second example shows how to use a data slot to do the same thing.
First Example
The following example shows how to use a field that is marked with ThreadStaticAttribute to hold thread-specific information. This technique provides better performance than the technique that is shown in the second example.
Imports System Imports System.Threading Class Test <MTAThread> _ Shared Sub Main() For i As Integer = 1 To 3 Dim newThread As New Thread(AddressOf ThreadData.ThreadStaticDemo) newThread.Start() Next i End Sub End Class Class ThreadData <ThreadStaticAttribute> _ Shared threadSpecificData As Integer Shared Sub ThreadStaticDemo() ' Store the managed thread id for each thread in the static ' variable. threadSpecificData = Thread.CurrentThread.ManagedThreadId ' Allow other threads time to execute the same code, to show ' that the static data is unique to each thread. Thread.Sleep( 1000 ) ' Display the static data. Console.WriteLine( "Data for managed thread {0}: {1}", _ Thread.CurrentThread.ManagedThreadId, threadSpecificData ) End Sub End Class ' This code example produces output similar to the following: ' 'Data for managed thread 4: 4 'Data for managed thread 5: 5 'Data for managed thread 3: 3
using System; using System.Threading; class Test { static void Main() { for(int i = 0; i < 3; i++) { Thread newThread = new Thread(ThreadData.ThreadStaticDemo); newThread.Start(); } } } class ThreadData { [ThreadStaticAttribute] static int threadSpecificData; public static void ThreadStaticDemo() { // Store the managed thread id for each thread in the static // variable. threadSpecificData = Thread.CurrentThread.ManagedThreadId; // Allow other threads time to execute the same code, to show // that the static data is unique to each thread. Thread.Sleep( 1000 ); // Display the static data. Console.WriteLine( "Data for managed thread {0}: {1}", Thread.CurrentThread.ManagedThreadId, threadSpecificData ); } } /* This code example produces output similar to the following: Data for managed thread 4: 4 Data for managed thread 5: 5 Data for managed thread 3: 3 */
using namespace System; using namespace System::Threading; ref class ThreadData { private: [ThreadStaticAttribute] static int threadSpecificData; public: static void ThreadStaticDemo() { // Store the managed thread id for each thread in the static // variable. threadSpecificData = Thread::CurrentThread->ManagedThreadId; // Allow other threads time to execute the same code, to show // that the static data is unique to each thread. Thread::Sleep( 1000 ); // Display the static data. Console::WriteLine( "Data for managed thread {0}: {1}", Thread::CurrentThread->ManagedThreadId, threadSpecificData ); } }; int main() { for ( int i = 0; i < 3; i++ ) { Thread^ newThread = gcnew Thread( gcnew ThreadStart( ThreadData::ThreadStaticDemo )); newThread->Start(); } } /* This code example produces output similar to the following: Data for managed thread 4: 4 Data for managed thread 5: 5 Data for managed thread 3: 3 */
Second Example
The following example demonstrates how to use a named data slot to store thread-specific information.
Note
|
|---|
|
The example code does not use the AllocateNamedDataSlot method, because the GetNamedDataSlot method allocates the slot if it has not already been allocated. If the AllocateNamedDataSlot method is used, it should be called in the main thread at program startup. |
Imports System Imports System.Threading Class Test Public Shared Sub Main() Dim newThreads(3) As Thread Dim i As Integer For i = 0 To newThreads.Length - 1 newThreads(i) = _ New Thread(New ThreadStart(AddressOf Slot.SlotTest)) newThreads(i).Start() Next i Thread.Sleep(2000) For i = 0 To newThreads.Length - 1 newThreads(i).Join() Console.WriteLine("Thread_{0} finished.", _ newThreads(i).ManagedThreadId) Next i End Sub End Class Class Slot Private Shared randomGenerator As New Random() Public Shared Sub SlotTest() ' Set random data in each thread's data slot. Dim slotData As Integer = randomGenerator.Next(1, 200) Dim threadId As Integer = Thread.CurrentThread.ManagedThreadId Thread.SetData( Thread.GetNamedDataSlot("Random"), slotData) ' Show what was saved in the thread's data slot. Console.WriteLine("Data stored in thread_{0}'s data slot: {1,3}", threadId, slotData) ' Allow other threads time to execute SetData to show ' that a thread's data slot is unique to itself. Thread.Sleep(1000) Dim newSlotData As Integer = _ CType(Thread.GetData(Thread.GetNamedDataSlot("Random")), Integer) If newSlotData = slotData Then Console.WriteLine("Data in thread_{0}'s data slot is still: {1,3}", threadId, newSlotData) Else Console.WriteLine("Data in thread_{0}'s data slot changed to: {1,3}", threadId, newSlotData) End If End Sub End Class
using System; using System.Threading; class Test { public static void Main() { Thread[] newThreads = new Thread[4]; int i; for (i = 0; i < newThreads.Length; i++) { newThreads[i] = new Thread(new ThreadStart(Slot.SlotTest)); newThreads[i].Start(); } Thread.Sleep(2000); for (i = 0; i < newThreads.Length; i++) { newThreads[i].Join(); Console.WriteLine("Thread_{0} finished.", newThreads[i].ManagedThreadId); } } } class Slot { private static Random randomGenerator = new Random(); public static void SlotTest() { // Set random data in each thread's data slot. int slotData = randomGenerator.Next(1, 200); int threadId = Thread.CurrentThread.ManagedThreadId; Thread.SetData( Thread.GetNamedDataSlot("Random"), slotData); // Show what was saved in the thread's data slot. Console.WriteLine("Data stored in thread_{0}'s data slot: {1,3}", threadId, slotData); // Allow other threads time to execute SetData to show // that a thread's data slot is unique to itself. Thread.Sleep(1000); int newSlotData = (int)Thread.GetData(Thread.GetNamedDataSlot("Random")); if (newSlotData == slotData) { Console.WriteLine("Data in thread_{0}'s data slot is still: {1,3}", threadId, newSlotData); } else { Console.WriteLine("Data in thread_{0}'s data slot changed to: {1,3}", threadId, newSlotData); } } }
using namespace System; using namespace System::Threading; ref class Slot { private: static Random^ randomGenerator = gcnew Random(); public: static void SlotTest() { // Set random data in each thread's data slot. int slotData = randomGenerator->Next(1, 200); int threadId = Thread::CurrentThread->ManagedThreadId; Thread::SetData( Thread::GetNamedDataSlot("Random"), slotData); // Show what was saved in the thread's data slot. Console::WriteLine("Data stored in thread_{0}'s data slot: {1,3}", threadId, slotData); // Allow other threads time to execute SetData to show // that a thread's data slot is unique to itself. Thread::Sleep(1000); int newSlotData = (int)Thread::GetData(Thread::GetNamedDataSlot("Random")); if (newSlotData == slotData) { Console::WriteLine("Data in thread_{0}'s data slot is still: {1,3}", threadId, newSlotData); } else { Console::WriteLine("Data in thread_{0}'s data slot changed to: {1,3}", threadId, newSlotData); } } }; ref class Test { public: static void Main() { array<Thread^>^ newThreads = gcnew array<Thread^>(4); int i; for (i = 0; i < newThreads->Length; i++) { newThreads[i] = gcnew Thread(gcnew ThreadStart(&Slot::SlotTest)); newThreads[i]->Start(); } Thread::Sleep(2000); for (i = 0; i < newThreads->Length; i++) { newThreads[i]->Join(); Console::WriteLine("Thread_{0} finished.", newThreads[i]->ManagedThreadId); } } }; int main() { Test::Main(); }
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Important
Note