Thread.AllocateNamedDataSlot(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在所有线程上分配已命名的数据槽。 为了获得更好的性能,请改用以 ThreadStaticAttribute 特性标记的字段。
public:
static LocalDataStoreSlot ^ AllocateNamedDataSlot(System::String ^ name);
public static LocalDataStoreSlot AllocateNamedDataSlot (string name);
static member AllocateNamedDataSlot : string -> LocalDataStoreSlot
Public Shared Function AllocateNamedDataSlot (name As String) As LocalDataStoreSlot
参数
- name
- String
要分配的数据槽的名称。
返回
所有线程上已分配的命名数据槽。
例外
已存在具有指定名称的命名的数据槽。
示例
本部分包含两个代码示例。 第一个示例演示如何使用用 ThreadStaticAttribute 属性标记的字段来保存特定于线程的信息。 第二个示例演示如何使用数据槽执行相同操作。
第一个示例
以下示例演示如何使用标记为 ThreadStaticAttribute 的字段来保存特定于线程的信息。 与第二个示例中所示的技术相比,此方法的性能更好。
using namespace System;
using namespace System::Threading;
ref class ThreadData
{
private:
[ThreadStatic]
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
*/
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
{
[ThreadStatic]
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
*/
open System
open System.Threading
type ThreadData() =
// Create a static variable to hold the data for each thread.
[<ThreadStatic; DefaultValue>]
static val mutable private threadSpecificData : int
static member ThreadStaticDemo() =
// Store the managed thread id for each thread in the static
// variable.
ThreadData.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.
printfn $"Data for managed thread {Thread.CurrentThread.ManagedThreadId}: {ThreadData.threadSpecificData}"
for i = 0 to 2 do
let newThread = Thread 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
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
<ThreadStatic> _
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
第二个示例
以下示例演示如何使用命名数据槽来存储特定于线程的信息。
注意
示例代码不使用 AllocateNamedDataSlot 方法,因为 GetNamedDataSlot 该方法会分配槽(如果尚未分配)。 AllocateNamedDataSlot如果使用 方法,则应在程序启动时在主线程中调用它。
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();
}
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);
}
}
}
open System
open System.Threading
module Slot =
let private randomGenerator = Random()
let slotTest () =
// Set random data in each thread's data slot.
let slotData = randomGenerator.Next(1, 200)
let threadId = Thread.CurrentThread.ManagedThreadId
Thread.SetData(Thread.GetNamedDataSlot "Random", slotData)
// Show what was saved in the thread's data slot.
printfn $"Data stored in thread_{threadId}'s data slot: {slotData, 3}"
// Allow other threads time to execute SetData to show
// that a thread's data slot is unique to itself.
Thread.Sleep 1000
let newSlotData = Thread.GetData(Thread.GetNamedDataSlot "Random") :?> int
if newSlotData = slotData then
printfn $"Data in thread_{threadId}'s data slot is still: {newSlotData, 3}"
else
printfn $"Data in thread_{threadId}'s data slot changed to: {newSlotData, 3}"
let newThreads =
[| for _ = 0 to 3 do
let thread = Thread Slot.slotTest
thread.Start()
thread |]
Thread.Sleep 2000
for tread in newThreads do
tread.Join()
printfn $"Thread_{tread.ManagedThreadId} finished."
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
注解
重要
.NET Framework提供了两种机制,用于使用线程本地存储 (TLS) :线程相对静态字段 (即用属性) 和数据槽标记ThreadStaticAttribute的字段。 与数据槽相比,线程相对静态字段的性能要好得多,并启用编译时类型检查。 有关使用 TLS 的详细信息,请参阅 线程本地存储:Thread-Relative静态字段和数据槽。
线程使用本地存储内存机制来存储特定于线程的数据。 公共语言运行时在创建时将多槽数据存储数组分配给每个进程。 线程可以在数据存储区中分配数据槽,在槽中存储和检索数据值,并在线程过期后释放该槽以供重复使用。 每个线程的数据槽是唯一的。 其他任何线程 (甚至子线程) 都无法获取该数据。
无需使用 AllocateNamedDataSlot 方法分配命名数据槽,因为 GetNamedDataSlot 该方法会分配槽(如果尚未分配)。
注意
AllocateNamedDataSlot如果使用 方法,则应在程序启动时在主线程中调用该方法,因为如果已分配具有指定名称的槽,该方法将引发异常。 无法测试槽是否已分配。
使用此方法分配的槽必须使用 释放 FreeNamedDataSlot。