Correct way to implement a resource pool
问题 I'm trying to implement something that manages a pool of resources such that the calling code can request an object and will be given one from the pool if it's available, or else it will be made to wait. I'm having trouble getting the synchronization to work correctly however. What I have in my pool class is something like this (where autoEvent is an AutoResetEvent initially set as signaled: public Foo GetFooFromPool() { autoEvent.WaitOne(); var foo = Pool.FirstOrDefault(p => !p.InUse); if