One lock per slice
Lock striping is the general technique behind a concurrent hash map. You allocate a fixed array of locks, called stripes, and map each piece of data to a stripe by hashing its identity. An operation locks only the stripe its data belongs to.
Why a fixed count
The stripe array stays a constant size even as the structure grows:
- A table of millions of buckets might use only sixteen or sixty four stripes
- Each stripe guards many buckets, but the mapping is stable, so a key always resolves to the same lock
- A small fixed array keeps memory bounded and avoids creating a lock per element
The trade off
Striping reduces contention without eliminating it. Two unrelated keys can still collide on the same stripe and serialize, but the odds shrink as you add stripes. Operations that span multiple stripes, such as resizing or a global size count, must acquire locks in a consistent order to avoid deadlock.
Key idea
Lock striping maps data to a fixed array of locks, cutting contention while keeping lock memory bounded and the mapping stable.