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

前端 未结 3 697
予麋鹿
予麋鹿 2021-02-19 04:10

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 ther

3条回答
  •  天命终不由人
    2021-02-19 04:12

    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...

提交回复
热议问题