thread-static

Is this a thread safe way to initialize a [ThreadStatic]?

[ThreadStatic] private static Foo _foo; public static Foo CurrentFoo { get { if (_foo == null) { _foo = new Foo(); } return _foo; } } Is the previous code thread safe? Or do we need to lock the method? If its ThreadStatic there's one copy per thread. So, by definition, its thread safe. This blog has...

Using Parallel Extensions with ThreadStatic attribute. Could it leak memory?

I'm using Parallel Extensions fairly heavily and I've just now encountered a case where using thread local storage might be sensible to allow re-use of objects by worker threads. As such I was looking at the ThreadStatic attribute which marks a static field/variable as having a unique value per...

Will values in my ThreadStatic variables still be there when cycled via ThreadPool?

I am using ThreadStatic variables to store some data, but I am worried that the data I store on the thread will still be there after I am finished with it and release back to the ThreadPool. Do I need to worry about clearing my ThreadStatic variables before I am finished with the thread? Or will the...