System.Threading.Thread.AllocateNamedDataSlot Method

Allocates a named data slot on all threads. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.

Syntax

public static LocalDataStoreSlot AllocateNamedDataSlot (string name)

Parameters

name
The name of the data slot to be allocated.

Returns

The allocated named data slot on all threads.

Remarks

Note:

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 Thread.AllocateNamedDataSlot(string) method to allocate a named data slot, because the Thread.GetNamedDataSlot(string) method allocates the slot if it has not already been allocated.

Note:

If the Thread.AllocateNamedDataSlot(string) 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 Thread.FreeNamedDataSlot(string).

Requirements

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0