Questions tagged [threadstatic]

An attribute indicating that the value of a static field is unique for each thread.

An attribute indicating that the value of a static field is unique for each thread.

References

55 questions
157
votes
5 answers

How does the ThreadStatic attribute work?

How does [ThreadStatic] attribute work? I assumed that the compiler would emit some IL to stuff/retrieve the value in the TLS, but looking at a disassembly it doesn't seem to do it at that level. As a follow up, what happens if you put it on a…
joshperry
  • 41,167
  • 16
  • 88
  • 103
107
votes
3 answers

ThreadStatic v.s. ThreadLocal: is generic better than attribute?

[ThreadStatic] is defined using attribute while ThreadLocal uses generic. Why different design solutions were chosen? What are the advantages and disadvantages of using generic over attributes in this case?
user2341923
  • 4,537
  • 6
  • 30
  • 44
53
votes
2 answers

Initializing ThreadStatic field still causes NullReferenceException

I've written myself a multi-threaded random generator public static class MyRandGen { private static Random GlobalRandom = new Random(); [ThreadStatic] private static Random ThreadRandom = new Random(SeedInitializer()); private…
Tarec
  • 3,268
  • 4
  • 30
  • 47
29
votes
2 answers

What's the effect of AsyncLocal in non async/await code?

I'm working on a very large and old code base of a desktop winform application. In this code base there are lots of operations performed in background threads, mainly using BackgroundWorker. A common pattern in this code base, is to hide complexity…
Fede
  • 3,928
  • 1
  • 20
  • 28
25
votes
2 answers

ThreadStatic vs. ThreadLocal Performance: speedups or alternatives?

I recently read this post about poor performance of fields marked ThreadStatic - they're apparently 60x slower than normal field access. Does .NET 4's ThreadLocal< T > perform any better? Are there any alternatives that offer high performance…
Mark
  • 9,320
  • 6
  • 57
  • 70
21
votes
2 answers

ThreadStaticAttribute in ASP.NET

I have a component that needs to store static values fore each thread. It's a general component that can be used in many scenarios and not only in ASP.NET. I was thinking to use the [ThreadStatic] attribute to achieve my goal. Supposing that it…
gsharp
  • 27,557
  • 22
  • 88
  • 134
14
votes
2 answers

.NET: ThreadStatic vs lock { }. Why ThreadStaticAttribute degrades performance?

I've written small test program and was surprised why lock {} solution performs faster than lock-free but with [ThreadStatic] attribute over static variable. [ThreadStatic] snippet: [ThreadStatic] private static long ms_Acc; public static void…
Roman
  • 4,531
  • 10
  • 40
  • 69
14
votes
3 answers

Using ThreadStatic to replace expensive locals -- good idea?

Update: as I should have expected, the community's sound advice in response to this question was to "measure it and see." chibacity posted an answer with some really nice tests that did this for me; meanwhile, I wrote a test of my own; and the…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
10
votes
1 answer

asp.net mvc3 request thread affinity

I am using a proprietary IoC mechanism in my asp.net mvc3 application (on IIS7) that saves state in [ThreadStatic] fields and therefore relies on an assumption that HttpApplication.BeginRequest, HttpApplication.EndRequest and the whole synchronous…
user1088045
  • 294
  • 1
  • 2
  • 11
10
votes
2 answers

How to maintain Thread context across async await model in C#?

Is using ThreadStatic and setting the context every time await completes "an option"? Is there another way? public async void Test() { // This is in Thread 1 Foo foo = new Foo(); Context.context = "context1"; // This is ThreadStatic …
Ashwin
  • 121
  • 1
  • 4
7
votes
1 answer

ASP.NET and ThreadStatic as part of TransactionScope's implementation

I was wondering how TransactionScope class works to keep the transaction between different method calls (without the need to pass it as a parameter) and I came to this doubt. I've got two considerations about this question: 1 Looking into…
Fabio
  • 3,020
  • 4
  • 39
  • 62
7
votes
1 answer

Accessing ThreadStatic field from .NET profiler

This question in stackoverflow asks how [ThreadStatic] is implemented: How does the ThreadStatic attribute work? Some suggested that it should be viewed as an extension of the Thread object. I'm not sure if that means it is based on win32 TLS. My…
user967710
  • 1,815
  • 3
  • 32
  • 58
6
votes
1 answer

Thread Static, ASP.NET and Async handlers

Please consider these sceanrios: An async .ashx handler A async .asmx web-service method A sync MVC 5 controller action method I am trying to figure out a way to set "logical thread" specific data that can be accessed consistently during a…
r_honey
  • 883
  • 4
  • 15
  • 31
6
votes
1 answer

Inheriting ThreadStatic values to implement dynamic scoping in C#/.NET in multithreaded context

Is there a way to make newly-spawned threads inherit the values of ThreadStatic state (or something like it) on their parent threads? I would like to use this (or something like it) to implement "dynamically scoped" special variables that contain…
Max Strini
  • 2,288
  • 21
  • 24
6
votes
2 answers

C# ThreadStatic + volatile members not working as expected

I was reading through the tips and tricks post and I thought I'd try out some of the C# stuff that I'd never done before. Therefore, the following code serves no actual purpose, but is just a 'test function' to see what happens. Anyway, I have two…
Xenoprimate
  • 7,691
  • 15
  • 58
  • 95
1
2 3 4