Thread::AllocateDataSlot Method ()
Allocates an unnamed data slot on all the threads. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.
Assembly: mscorlib (in mscorlib.dll)
public: [HostProtectionAttribute(SecurityAction::LinkDemand, SharedState = true, ExternalThreading = true)] static LocalDataStoreSlot^ AllocateDataSlot()
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. |
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.
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.
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 code example demonstrates how to use a data slot to store thread-specific information.
using namespace System; using namespace System::Threading; ref class Slot { private: static Random^ randomGenerator; static LocalDataStoreSlot^ localSlot; static Slot() { randomGenerator = gcnew 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() ); } }; int main() { array<Thread^>^newThreads = gcnew array<Thread^>(4); for ( int i = 0; i < newThreads->Length; i++ ) { newThreads[ i ] = gcnew Thread( gcnew ThreadStart( &Slot::SlotTest ) ); newThreads[ i ]->Start(); } }
Available since 1.1
