IT lexicon › Programming › Recursive mutex

Recursive mutex

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

A lock the same thread can acquire several times without deadlocking itself, released only once it has been unlocked as many times.

Take an ordinary mutex twice from the same thread and you have a one-line deadlock: the thread waits for itself. The recursive kind tracks an owner and a count, so a function holding the lock can call another function that takes it too. POSIX offers PTHREAD_MUTEX_RECURSIVE, FreeRTOS has xSemaphoreTakeRecursive(), and in both cases every take must be matched by a give.

They are about equally convenient and distrusted. A recursive mutex hides the fact that the code doesn't know whether it already holds the lock, and the inner call may see protected data in a half-updated state. David Butenhof, one of the designers of POSIX threads, has called them a crutch. The clean fix is to split the code into a locking outer function and an unlocked inner one.

← Back to the lexicon