探秘System.Threading系列 第三篇:Thread的数据ThreadStatic 和LocalDataStoreSlot

线程独享的数据,可以通过Thread类的静态方法SetData和GetData方法实现,也可以通过静态变量加上[ThreadStatic]特性。

Msdn推荐优先使用ThreadStatic修饰静态字段的方法,只有在数据的类型无法确定时再使用数据槽LocalDataStoreSlot和SetData,GetData方法。后者在性能上不如前者,而且使用复杂。

原文地址:https://www.cnblogs.com/yukaizhao/p/thread_setdata_getdata_threadstatic.html