I have a static class that holds a dictionary of objects. I have multiple threads that access this dictionary and create objects on it that are specific to that thread. How and when can I remove entries from the dictionary when a thread is “done”?
Basically this is a static class for an ASP.NET application that all requests can have “isolated objects” (that are related to the current thread) created in the dictionary. But, I don’t really want to be tied to ASP.NET.
Does this make sense?
You can decorate a static field with a
[ThreadStatic]attribute and each thread will see its own value. If you are using .NET 4.0, you can useThreadLocal<T>to do that too. Basically, you’d have a separate dictionary for each thread holding its own objects rather than saving all objects in a single dictionary.