79240384

Date: 2024-11-30 19:56:48
Score: 0.5
Natty:
Report link

I know I am playing a thread necromancer right here but I was researching this myself and just ended up reading the source code for ThreadLocal here, on mscorlib.

In the source code we can see that ThreadLocal is using a [ThreadStatic] attribute on ts_slotArray field. While this attribute has no implementation, the compiler upon encountering it on some field creates a thread specific storage slot for that variable and thus even if your ThreadLocal is static, it doesn't matter as compiler will disregard this and generate code that still creates a thread specific storage slot for that value.

If you want to keep your values reusable, you could use a ConcurrentBag/Dictionary or create your own thread safe collection.

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (0.5):
Posted by: Karol HaliƄski