thread-static

Deterministic dispose of ThreadStatic objects

The ThreadStatic attribute declares a static variable as unique-per-thread. Do you know an easy pattern to correctly dispose such variables? What we used before ThreadStatic is a ThreadContextManager. Every thread was allocated a ThreadContext which retained all thread-specific information. We spawned some threads and let them work. The...

Thread Static variables for asynchronous operations in .NET

Is there any way to have ThreadStatic variables be transferred from one thread to another? I have a bunch of ThreadStatic variables, and now that I am converting my operation to be asynchronous, I want to be able to "transfer" them from the first thread (where they are set) to the callback thread (where they will be read). Is this poss...

Seeking One-Size-Fits-All Context Based Storage

First off, I wish context based storage was consistent across the framework! With that said, I'm looking for an elegant solution to make these properties safe across ASP.NET, WCF and any other multithreaded .NET code. The properties are located in some low-level tracing helpers (these are exposed via methods if you're wondering why they...

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 ThreadPool do this for me before "pa...

ThreadStatic Modified with Static C#

I have some code where I use a thread static object in C#. [ThreadStatic] private DataContext connection I was wondering, in this case, what if any change would I get if I put the static modifier on the thread static context? [ThreadStatic] private static DataContext connection With the first would there be one copy of the context ...

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

How is Java's ThreadLocal implemented under the hood?

How is ThreadLocal implemented? Is it implemented in Java (using some concurrent map from ThreadID to object), or does it use some JVM hook to do it more efficiently? ...

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 thread. It seems to me that it would be un...