NSRecursiveLock

A recursive mutex implementation that allows the same thread to acquire the lock multiple times without deadlocking.

Base Class

NSLock

Instance Variables

mulle_atomic_pointer_t   _thread;  // Current owning thread
mulle_atomic_pointer_t   _depth;   // Lock recursion depth

Methods

All methods are marked with MULLE_OBJC_THREADSAFE_METHOD:

Locking Operations

Usage Example

NSRecursiveLock *lock = [NSRecursiveLock new];

// Same thread can acquire multiple times
[lock lock];
[lock lock];  // Increments depth counter
// ... critical section ...
[lock unlock];  // Decrements depth counter
[lock unlock];  // Releases lock

// Try lock without blocking
if ([lock tryLock])
{
   if ([lock tryLock])  // Can acquire again
   {
      // ... nested critical section ...
      [lock unlock];
   }
   [lock unlock];
}

Important Notes

  1. Thread Safety
  2. Implementation Details