ThreadStaticAttribute is not shared between threads. Each executing thread has a separate instance of the field, and independently sets and gets values for that field. If the field is accessed on a different thread, it will contain a different value. The ThreadStatic attribute is only useful when you control the ThreadPool (and the lifecycle of the threads). It also reminds one to think about variable scope/lifetime especially in the context of ASP.NET.
Questions tagged [thread-static]
9 questions
86
votes
5 answers
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?

ripper234
- 222,824
- 274
- 634
- 905
54
votes
3 answers
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…

Anthony D
- 10,877
- 11
- 46
- 67
15
votes
2 answers
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…

skb
- 30,624
- 33
- 94
- 146
10
votes
1 answer
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…

ripper234
- 222,824
- 274
- 634
- 905
10
votes
2 answers
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?

Shane Courtrille
- 13,960
- 22
- 76
- 113
6
votes
2 answers
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…

redcalx
- 8,177
- 4
- 56
- 105
5
votes
2 answers
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…

jsw
- 1,752
- 1
- 14
- 20
2
votes
4 answers
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…

skb
- 30,624
- 33
- 94
- 146
0
votes
2 answers
Why do I get NullReferenceException in the following code, C#?
I have the following test program where I an using a ThreadStatic variable, when I try this code I get a NullReferenceException .
using System;
using System.Threading;
namespace MiscTests
{
public class Person
{
public string Name {…

ShrShr
- 389
- 3
- 10