Lock striping

In the previous section, I recommended using one lock per shared resource. We also saw that sometimes we cannot do such fine-grained locking and that we have to implement locks that protect more than one resource at the same time.

The lock striping pattern covers the opposite case. Sometimes, one lock per resource is not enough. On some occasions, we may want to implement multiple locks for one shared resource. We could, for example, add a lock to each element in an array or list.

Imagine the fitting rooms in a clothing store. They are not protected with one master lock as that would prevent multiple customers from trying out clothes at the same time. Rather, each room has its own lock.

This pattern can only be applied when ...

Get Mastering Delphi Programming: A Complete Reference Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.