Friday, April 24, 2015

Classes that are not thread safe

Hi Wyck,
That actually answers my question about thread local storage. So, if I use ThreadLocal<T>(Func<T>) to initialize a field to a collection of some type for example, if the class implements IDisposable how does it get access to each threads copy of the field when disposing? Or better put, what is the pattern for dealing with tls when disposing?

Thanks!

There's no change in the pattern. The TLS just holds a thread-local reference to an object.  All multithreaded issues with respect to disposal of that object still apply.  Take steps to ensure you know which thread is disposing the object.

No comments:

Post a Comment