开发者

How to create a thread-safe pool of objects?

开发者 https://www.devze.com 2022-12-12 07:48 出处:网络
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

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 WeakReferences are stored, but other than that, it's running in production with no problems at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消