LocalDataStoreSlot 表示線程槽,封裝內存槽以存儲本地數據(MSDN)
.NET Framework 提供了兩個使用線程本地存儲區 (TLS) 的機制:線程相關的靜態字段和數據槽。
-
線程相關的靜態字段是用 ThreadStaticAttribute 特性標記的 static 字段(Visual Basic 中的 Shared 字段)。 這些字段提供的性能比數據槽的性能好,而且它還啓用了編譯時類型檢查。
-
與線程相關的靜態字段相比,數據槽性能較低,並且比較難使用。 此外,數據被存儲爲 Object 類型,因此在使用之前必須將其強制轉換爲正確的類型。 但是,如果在編譯時所具有的信息不足以分配靜態字段,則可以使用數據槽。
LocalDataStoreSlot 結構可用作本地存儲內存機制,線程和上下文可以使用此機制分別存儲線程特定的數據和上下文特定的數據。 公共語言運行時在創建每個進程時給它分配一個多槽數據存儲區數組。 線程或上下文調用各種函數在數據存儲區中分配數據槽、在槽內存儲和檢索數據值、以及釋放數據槽以便在線程或上下文過期後重新使用它。
實例如下:
static void Main(string[] args)
{
for (int num = 0; num < 10; num++)
{
Thread thread = new Thread(FatherThread);
thread.Start(num);
}
Console.ReadKey();
}
/// <summary>
/// the method for the father thread.
/// </summary>
public static void FatherThread(object num)
{
name = num + Thread.CurrentThread.ToString();
LocalDataStoreSlot slot = Thread.AllocateNamedDataSlot("slot");
//設置TLS中的值
Thread.SetData(slot, name);
Thread threadChild = new Thread(ChildThread);
threadChild.Start(num);
}
public static void ChildThread(object num)
{
LocalDataStoreSlot slot = Thread.AllocateNamedDataSlot("slot");
string value = Thread.GetData(slot).ToString();
Console.WriteLine(value);
}
static void Main(string[] args)
{
for (int num = 0; num < 10; num++)
{
Thread thread = new Thread(FatherThread);
thread.Start(num);
}
Console.ReadKey();
}
/// <summary>
/// the method for the father thread.
/// </summary>
public static void FatherThread(object num)
{
name = num + Thread.CurrentThread.ToString();
SetName(num.ToString(),name);
Thread threadChild = new Thread(ChildThread);
threadChild.Start(num);
}
public static void ChildThread(object num)
{
Console.WriteLine(GetName(num.ToString()));
}
public static string GetName(string name)
{
return CallContext.LogicalGetData(name).ToString();
}
public static void SetName(string name,string value)
{
CallContext.LogicalSetData(name, value);
}