Thread.AllocateDataSlot Method
Allocates an unnamed data slot on all the threads.
[Visual Basic] Public Shared Function AllocateDataSlot() As LocalDataStoreSlot [C#] public static LocalDataStoreSlot AllocateDataSlot(); [C++] public: static LocalDataStoreSlot* AllocateDataSlot(); [JScript] public static function AllocateDataSlot() : LocalDataStoreSlot;
Return Value
Remarks
The slot is allocated on all threads.
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.
Example
[Visual Basic, C#, C++] The following code example demonstrates how to use a data slot to store thread-specific information.
[Visual Basic] Imports System Imports System.Threading Class Test Shared Sub Main() Dim newThreads(3) As Thread For i As Integer = 0 To newThreads.Length - 1 newThreads(i) = New Thread(AddressOf Slot.SlotTest) newThreads(i).Start() Next i End Sub End Class Public Class Slot Shared randomGenerator As Random Shared localSlot As LocalDataStoreSlot Shared Sub New() randomGenerator = new Random() localSlot = Thread.AllocateDataSlot() End Sub Shared Sub SlotTest() ' Set different data in each thread's data slot. Thread.SetData(localSlot, randomGenerator.Next(1, 200)) ' Write the data from each thread's data slot. Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", _ AppDomain.GetCurrentThreadId().ToString(), _ Thread.GetData(localSlot).ToString()) ' Allow other threads time to execute SetData to show ' that a thread's data slot is unique to the thread. Thread.Sleep(1000) ' Write the data from each thread's data slot. Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", _ AppDomain.GetCurrentThreadId().ToString(), _ Thread.GetData(localSlot).ToString()) End Sub End Class [C#] using System; using System.Threading; class Test { static void Main() { Thread[] newThreads = new Thread[4]; for(int i = 0; i < newThreads.Length; i++) { newThreads[i] = new Thread( new ThreadStart(Slot.SlotTest)); newThreads[i].Start(); } } } class Slot { static Random randomGenerator; static LocalDataStoreSlot localSlot; static Slot() { randomGenerator = new Random(); localSlot = Thread.AllocateDataSlot(); } public static void SlotTest() { // Set different data in each thread's data slot. Thread.SetData(localSlot, randomGenerator.Next(1, 200)); // Write the data from each thread's data slot. Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", AppDomain.GetCurrentThreadId().ToString(), Thread.GetData(localSlot).ToString()); // Allow other threads time to execute SetData to show // that a thread's data slot is unique to the thread. Thread.Sleep(1000); Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", AppDomain.GetCurrentThreadId().ToString(), Thread.GetData(localSlot).ToString()); } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::Threading; __gc class Slot { static Random* randomGenerator; static LocalDataStoreSlot* localSlot; static Slot() { randomGenerator = new Random(); localSlot = Thread::AllocateDataSlot(); } public: static void SlotTest() { // Set different data in each thread's data slot. Thread::SetData(localSlot, __box(randomGenerator->Next(1, 200))); // Write the data from each thread's data slot. Console::WriteLine(S"Data in thread_{0}'s data slot: {1,3}", AppDomain::GetCurrentThreadId().ToString(), Thread::GetData(localSlot)->ToString()); // Allow other threads time to execute SetData to show // that a thread's data slot is unique to the thread. Thread::Sleep(1000); Console::WriteLine(S"Data in thread_{0}'s data slot: {1,3}", AppDomain::GetCurrentThreadId().ToString(), Thread::GetData(localSlot)->ToString()); } }; void main() { Thread* newThreads __gc[] = new Thread* __gc[4]; for(int i = 0; i < newThreads->Length; i++) { newThreads[i] = new Thread( new ThreadStart(0, &Slot::SlotTest)); newThreads[i]->Start(); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
See Also
Thread Class | Thread Members | System.Threading Namespace | Threads and Threading | Thread Local Storage and Thread-Relative Static Fields