1

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 frequently 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...

Hemant
  • 19,486
  • 24
  • 91
  • 127
  • 4
    Do NOT attempt to avoid explicit locks until you have EVIDENCE that locks are the #1 cause of UNACCEPTABLE performance. Locks are the vast majority of the time incredibly fast, and if your locks are slow then odds are good you have a deep design problem in your application. Remember, locks are only slow *under contention*, and if you have a lot of contention, *you're probably doing something wrong.* – Eric Lippert Nov 18 '09 at 15:14

2 Answers2

2

This post will be of interest:

ObjectPool<T> or similar for .NET already in a library?

Community
  • 1
  • 1
spender
  • 117,338
  • 33
  • 229
  • 351
1

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.

Kilhoffer
  • 32,375
  • 22
  • 97
  • 124