Does changing the culture of a threadpool thread affect it when it gets returned back to the pool?

人走茶凉 提交于 2019-11-28 07:33:33

问题


If I set the CurrentCulture of a thread pool thread, what happens when the thread finishes execution and gets returned back to the thread pool? Does it get its CurrentCulture reset back to the default (whatever that may mean), or will it retain the culture I have set on it?

I'm hoping that the framework resets the thread to a default state to guard against this, but cannot find any documentation to this effect. The closest I have found is from the MSDN docs for ThreadPool:

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.

This seems to indicate that the thread is not reset when it is returned.

I have tried some sample code to try to test this, and it does seem that the culture is reset, but I am not convinced that I am testing this behaviour correctly as I think I am only using a small subset of the ThreadPool's threads, and so cannot be sure I'm testing a thread that has already had it's culture set.


回答1:


I would not rely on the ThreadPool ever resetting information, specifically because of the text you quoted.

If you are worried about "changing" the thread pool's culture, I would make sure to reset it when your threaded task is complete. This is a simple enough task.

I do not believe the current threadpool does this, but even if it did, it would not be safe to assume that .NET 4+'s ThreadPool implementation will not change.



来源:https://stackoverflow.com/questions/1467194/does-changing-the-culture-of-a-threadpool-thread-affect-it-when-it-gets-returned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!