Does Thread.CurrentThread always return the same instance?

Can I do a Dictionary<Thread, object>thread storage and use Thread.CurrentThread to recover it?

+5
source share
2 answers

You can, but you will also need to synchronize (because Dictionary<,>it is not thread safe).

Alternative:

Of course, one of the advantages of using a dictionary compared to ThreadStaticAttibutethat is that you do not need to worry about garbage, not black magic. If you are using .NET 4 ThreadLocal<T>, perhaps the best option.

+10
source

, , ThreadStaticAttribute ThreadLocal, , .

+3

All Articles