开发者

Is there a straightforward way to have a thread-local instance variable?

开发者 https://www.devze.com 2023-01-01 22:06 出处:网络
With the ThreadStatic attribute I can have a static member of a class with one instance of the object per thread. This is really handy for achieving thread safety using types of objects that don\'t gu

With the ThreadStatic attribute I can have a static member of a class with one instance of the object per thread. This is really handy for achieving thread safety using types of objects that don't guarantee thread-safe instance methods (e.g., System.Random).

It only works for static members, though. Is there any straightforward way to declare a class member as thread-local, meaning, each class instance gets an object per t开发者_如何转开发hread?


Looks like the ThreadLocal<T> class is what I was looking for.

And yes, I do feel a bit stupid for not knowing about this before now.

0

精彩评论

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