Thread.GetNamedDataSlot(String)

https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.thread.getnameddataslot?redirectedfrom=MSDN&view=netframework-4.7.2#System_Threading_Thread_GetNamedDataSlot_System_String_

定义

查找命名的数据槽。 为了获得更好的性能,请改用以 ThreadStaticAttribute 特性标记的字段。

C#复制
public static LocalDataStoreSlot GetNamedDataSlot (string name);
参数
name
String

本地数据槽的名称。

返回

为此线程分配的 LocalDataStoreSlot

示例

本部分包含两个代码示例。 第一个示例演示如何使用标记有一个字段ThreadStaticAttribute属性用于保存特定于线程的信息。 第二个示例演示如何使用数据槽来执行相同的操作。

第一个示例

下面的示例演示如何使用字段标有ThreadStaticAttribute用于保存特定于线程的信息。 此方法可提供更好的性能比第二个示例所示的方法。

C#复制
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 */ 

第二个示例

下面的示例演示如何使用的命名的数据槽来存储特定于线程的信息。

C#复制
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); } } } 

注解

 重要

.NET Framework 提供了使用线程本地存储 (TLS) 的两种方式: 线程相对静态字段 (即,使用标记的字段ThreadStaticAttribute属性) 和数据槽。 线程相对静态字段提供更好的性能优于数据槽,并启用编译时类型检查。 有关使用 TLS 的详细信息,请参阅线程本地存储:线程相对静态字段和数据槽

线程使用本地存储内存机制来存储线程特定的数据。 在创建时,公共语言运行时分配将多个数据存储阵列分区到每个进程。 线程可以将数据存储区中的数据槽的分配、 存储和检索数据的槽中值以及该线程过期后释放以供重复使用的槽。 数据槽是每个线程的唯一的。没有其他线程 (甚至不是子线程) 可以获取该数据。

如果命名约定的位置不存在,将分配新的槽。 命名的数据槽是公共的可以通过任何人进行操作。

适用于

 

转载于:https://www.cnblogs.com/kelelipeng/p/10655263.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值