Multicore & Synchronization
Which correctly compares coarse-grained locking (one lock for a whole large data structure) with fine-grained locking (separate locks for each part)?
- ACoarse-grained locking always gives more parallelism
- BFine-grained locking reduces the number of locks
- CFine-grained locking lets threads use different parts concurrently for more parallelism, but design is harder and deadlock risk grows
Show answer and explanation
Answer: C. Fine-grained locking lets threads use different parts concurrently for more parallelism, but design is harder and deadlock risk grows
A coarse lock is simple and hard to get wrong, but threads using unrelated parts still wait for each other, so adding cores helps little. Finer granularity, such as one lock per hash-table bucket, lets different parts be operated on simultaneously. But situations requiring several locks increase, and managing acquisition order and the overhead of the locks themselves become issues.
- A. With a coarse lock, only one thread can enter at a time.
- B. The number of locks increases.
- C. Correct. The choice balances simplicity against parallelism.