liveBook · Manning

Fine-grained locking

As explained in Latency

Fine-grained locking is a synchronization technique that protects smaller portions of shared work or state with more numerous locks.

Fine-grained locking protects smaller portions of shared work or state with more numerous locks. Compared with coarse-grained locking, it allows more threads to proceed concurrently, but increases implementation complexity and synchronization overhead. section 9.2.1

Its main performance use is reducing contention: partition critical work so that threads hold individual locks for shorter periods rather than queueing behind one broad lock. This can reduce convoying, where a slow lock holder delays many waiting threads, increases latency, lowers throughput, and leaves CPUs underutilized. Fine-grained locking does not eliminate these problems, and wait-free synchronization may be preferable for strict latency requirements. section 8.2.3 section 9.2.1

Figure 9.2 The OS runs multiple threads at the same time (but not simultaneously) on a single CPU core by context-switching between them. In this example, every thread is run for exactly 4 milliseconds, and then the OS switches to another thread. In real-world systems, a thread might block to wait for I/O, for example, causing the OS to switch to another thread more quickly.
Figure 9.2: The OS runs multiple threads at the same time (but not simultaneously) on a single CPU core by context switching between then.

Use the scheduling figure to connect lock contention with the physical cost of thread execution and context switching on a single CPU core. section 9.2.1

How Latency covers it

How deep it goes
Explains the coarse-versus-fine trade-off and convoying mitigation, but stops short of implementation details or correctness techniques.
Examples
Uses thread-based concurrency, kernel scheduling, context switching, and lock convoying as practical performance contexts.
What it assumes
Assumes threads and synchronization primitives, while contrasting coarse-grained locking with finer lock partitioning.
What you can do after
Apply it to reduce lock contention and critical-section duration, while recognizing added complexity and synchronization overhead.
How it connects
Connects fine-grained locking with coarse-grained locking, thread synchronization, convoying, wait-free synchronization, and context switching.

Also explained in

Compare how 2 books explain fine-grained locking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Latency. The section links point to the parts of the book it draws on.