开发者

Does Thread.CurrentThread always return same instance?

Can I make Dictionary<Thread, object> to store thread's data and use Thread.Curre开发者_开发知识库ntThread to retreive it?


You can, but you'd also need to synchronize (as Dictionary<,> isn't thread-safe).

Alternatives:

  • ThreadStaticAttribute
  • ThreadLocal<T> (.NET 4)

Of course, one benefit of using a dictionary over ThreadStaticAttibute is that you don't need to worry about garbage as much, or indeed black magic. If you're using .NET 4 though, ThreadLocal<T> is possibly your best option.


Yes you could, if you want to access other threads' data, but you should take a look at ThreadStaticAttribute or ThreadLocal first, it's much better if threads only need to see their own data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜