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

Posted by Dan Tao on Stack Overflow See other posts from Stack Overflow or by Dan Tao
Published on 2010-06-03T14:30:17Z Indexed on 2010/06/07 17:12 UTC
Read the original article Hit count: 157

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 thread?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about multithreading