Performance - Lock Granularity


Within a modern multithreaded, multiprocessor system, shared data objects must be protected by software locks. These locks restrict the ability to modify the data to the one thread which holds the lock at any particular point in time. Other threads that wish to write to that same object must wait until the lock is released. The lock ensures that more than one thread is not modifying the same data object at the same time.

A key to good performance is the efficient matching of locks to shared data. If too much shared data is controlled by a single lock, the likelihood of lock contention increases. Lock contention occurs when more than one thread wishes to access the same shared data object. By improving the lock granularity, which is accomplished by reducing the amount of shared data associated with a particular lock, the amount of lock contention can be reduced. This improvement can become significant as the number of CPUs in the system increases.

Care must be taken when improving lock granularity. At times related data structures must be controlled by a single lock. In addition, the frequency of contention must be determined to ensure that the cost of acquiring multiple locks does not overcome the benefit of the improved granularity.