I'm looking at the code for the ConnectionManager<C> class and have some questions about locking.
One of the static GetManager method overloads does a lock, and the book says that this "ensures that only one thread at a time can attempt to create an open connection." Is there a reason that you'd want to prevent multiple threads from simultaneously creating an open connection? What's the harm in allowing them to do so? They are different connection instances.
Also, the DeRef method does another lock. I'm guessing that this is done to protect the mRefCount instance field, but since the ConnectionManager<C> instances are stored in thread-local-storage, I wouldn't think that DeRef on a particular instance would ever get called across threads anyway. Therefore, it seems that guarding mRefCount is unnecessary since it's not shared mutable data.
Since the synchronization object is the same for GetManager and DeRef, that would mean that one thread could be calling GetManager but would have to wait until another thread's execution of DeRef was complete. The two methods don't seem to be related, but since they lock on the same static object, one will have to (unnecessarily) wait for the other.
Unless I'm missing something, locking is not needed at all in this class, and its presence just leads to liveness issues.
Please clarify. Thanks!