Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 346
adrianbanks Avatar asked Sep 23 '09 16:09

adrianbanks


1 Answers

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.

like image 147
Reed Copsey Avatar answered Sep 20 '22 22:09

Reed Copsey