Friday, April 24, 2015

Classes that are not thread safe

Yes, there's a ThreadLocal<T> class.

In general way there's no way to cause a reference alone to be inaccessible by another thread, unless it's a ThreadLocal.  You'd have to intercept and deny a reference being passed to the wrong thread at the moment it is being passed.  You'll need to define your own logic to prevent it.

I would not trust any class to be "thread safe" unless it came with some kind of promise that it was.  Even then, I'd expect that promise to be fairly detailed and explicit.  In fact, I would hope (for performance reasons) that implementations are not trying to keep themselves "thread safe" (such as a List<T>, for example).

No comments:

Post a Comment