using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace DataSlot
{
class Program
{
static void Main(string[] args)
{
//分配一个命名的槽位
var slot = Thread.AllocateNamedDataSlot("username");
//分配一个不命名的槽位
var slot2 = Thread.AllocateDataSlot();
//释放槽位
Thread.FreeNamedDataSlot("username");
//主线程 上 设置槽位,, 也就是hello world 只能被主线程读取,其他线程无法读取
Thread.SetData(slot, "hello world!!!");
var t = new Thread(() =>
{
var obj = Thread.GetData(slot);
//新的子线程中无法访问主线程中的变量
Console.WriteLine("当前工作线程:{0}", obj);
});
t.Start();
var obj2 = Thread.GetData(slot);
Console.WriteLine("主线程:{0}", obj2);
Console.Read();
}
}
}
C#,线程槽位,命名槽位,数据访问,线程安全
本文介绍了如何在C#中使用线程数据槽位进行线程间数据隔离,包括分配、设置和释放槽位,以及在不同线程中访问槽位数据的限制。