Hi
I implementing a high performance thread safe component, using no lock statements, only volatile and Interlocked are used for performance reasons.
I have volatile reference-type member in a class, that contains thread safe instance. This instance is thread safe only for a couple of operations, and not for another. Because of that and for performance reasons, in some cases i prefer creating new instance instead of updating the original, and it really working faster, especially because i dont using any lock statements.
So the volatile member can be replaced in any time to another instance, the volatile keyword ensures that there will not be any problem with that in multithreaded environment.
This of course working very well, but the only problem with that is the garbage collection of the old instances. When tested my component for performance, i found that it spending too much time in garbage collection the released instances.
Now i searching a way to recycle the old instances. The problem is that i can't just take the old instance when replacing and just reset it's state because there may be another threads that still using this instance and i can't found a way (without lock) that guaranties that noone using this instance anymore.
How can i guaranty that there is no thread that using the old instance without lock statements? (volatile and Interlocked are preferred)
Thanks.