Reset ThreadLocal <T>

I initialize my ThreadLocal with a Factory Func.

I would also like the reset value to return to zero.

Maybe I'm doing something wrong, but if I set my threadLocal.Value = null, it still seems that threadLocal.IsValueCreated == true.

How can I really reset threadlocal so that it lazily regenerates the value when it is requested?

+3
source share
1 answer

Not that I knew.

You can always make it ThreadLocal<Lazy<T>>and set it to new Lazy<T>when you want to reset it. If this is what you use more than once, you may need to encapsulate it like ResettableThreadLocal<T>or something like that.

+3
source

Source: https://habr.com/ru/post/1791594/


All Articles