IT lexicon › Programming › Reentrant lock

Reentrant lock

Programming På svenska → Updated: 2026-11-15

A lock that the same thread can acquire multiple times without blocking, tracking acquisition depth and requiring an equal number of releases.

A common pitfall with mutexes is that a thread holding a lock may attempt to acquire the same lock again — which would deadlock. A reentrant lock (also called recursive) solves this by counting how many times the current thread has acquired it. Each acquire increments the counter; each release decrements it. The lock is truly free only when the counter reaches zero. This pattern enables safe recursive functions and nested critical sections without having to manually avoid self-deadlock.

Java provides ReentrantLock in java.util.concurrent (JDK 1.5, 2004), offering finer control than the synchronized keyword. C offers pthread_mutexattr_settype(PTHREAD_MUTEX_RECURSIVE). They are essential for libraries and recursive algorithms.

← Back to the lexicon