Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Thread.AllocateNamedDataSlot Method

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

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)
[<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.
ExceptionCondition
ArgumentException

A named data slot with the specified name already exists.

Important noteImportant

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.

NoteNote

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.

NoteNote

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.

No code example is currently available or this language may not be supported.

Second Example

The following example demonstrates how to use a named data slot to store thread-specific information.

NoteNote

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.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 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.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.