How to create a thread-safe pool of objects?
In my server application (written in C#), I need to create a pool of same type of objects. I pull an object from pool when I need, and it goes back to pool when it is no longer needed. Mechanism needs to be thread safe since different threads will be asking and submitting these objects. I understand that freq开发者_高级运维uently locking something to make thread-safe has an adverse effect on application performance. Can you suggest a design by which I can even avoid explicit locks?
Please can you suggest the way I can accomplish above? Ability to resize the pool (create additional if running short of objects) would be a great add-on.
Thanks in advance...
This post will be of interest:
ObjectPool<T> or similar for .NET already in a library?
I've had good luck with this one on CodeProject. I only had to make a minor tweak to how WeakReference
s are stored, but other than that, it's running in production with no problems at all.
精彩评论