liveBook · Manning

Reentrant lock

As explained in Advanced Algorithms and Data Structures

A reentrant lock is a lock that allows the thread holding it to acquire it repeatedly, tracking nested acquisitions with a counter until matching releases occur.

A lock protects a shared resource by allowing only one call to execute its protected code at a time while other calls wait. This prevents concurrent updates but can cause deadlocks or unnecessary delays when used incorrectly. section 7.7.2

A reentrant lock lets the thread that already holds a lock acquire it again. The nested operation can proceed instead of waiting for its own thread to release the lock. section 7.7.4

Nested cache operations

Suppose set acquires a write lock and then calls evictOneEntry. If evictOneEntry also acquires that write lock, a non-reentrant lock makes the thread wait for a lock that set cannot release until the call returns. A reentrant lock permits the nested acquisition, allowing eviction to finish and avoiding this deadlock. section 7.7.4

How Advanced Algorithms and Data Structures covers it

How deep it goes
Defines locking and demonstrates same-thread reacquisition, but defers detailed lock internals and deadlock prevention.
Examples
Uses concurrent set calls and a set-to-evictOneEntry call chain to illustrate waiting and nested lock acquisition.
What it assumes
Builds from the basic idea of a lock and assumes familiarity with concurrent method calls and shared resources.
What you can do after
Apply it via Java's ReentrantReadWriteLock when nested cache operations require flexible synchronization.
How it connects
Connects concurrency, synchronization, deadlock, read/write locks, and cache eviction.

Also explained in

Compare how 2 books explain reentrant lock →

Related concepts

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