How to combine all values from a ThreadLocal<T>?
Since .NET 4.5, Microsoft added an attribute called Values
to the ThreadLocal
class that does exactly what you need. Here's the way it works:
var localResult = new ThreadLocal<int>(() => 0, trackAllValues: true);
Parallel.For(0, 10000, i =>
{
localResult.Value += Compute(i);
});
int result = localResult.Values.Sum();
The code above was obtained from the blog post: New in .NET 4.5: ThreadLocal.Values
This information is not available in .NET 4.0. For 4.5 and up, see Joao's answer.
Microsoft is considering adding such a feature. You will need to write a wrapper around ThreadLocal<T>
to add this behavior.
Here is a blog post that gives some directions about how to write a wrapper.