Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .NET ThreadPool thread get reset when it goes back to the pool?

When a thread pool thread is done, does stuff like Name or thread local data get reset? So when the thread comes out of the pool next time, it's like brand new?

Is there an "official" documentation on this aspect of the ThreadPool threads?

like image 320
Jiho Han Avatar asked Aug 04 '11 16:08

Jiho Han


1 Answers

It does NOT clear thread local storage when it's released, which is the most important aspect to note.

http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx

When the thread pool reuses a thread, it does not clear the data in thread local storage or in fields that are marked with the ThreadStaticAttribute attribute. Therefore, data that is placed in thread local storage by one method can be exposed to any other method that is executed by the same thread pool thread. A method that accesses a field that is marked with the ThreadStaticAttribute attribute could encounter different data depending on which thread pool thread executes it.

This is something to be very careful about...

like image 108
Jeff Avatar answered Sep 20 '22 10:09

Jeff