Inherits From: NSObject
Conforms To: NSLocking NSObject (NSObject)
Declared In: Foundation/NSLock.h
NSRecursiveLock is used for locks that need to be reacquired by the same thread.
An NSRecursiveLock locks a critical section of code such that a single thread can reaquire the lock multiple times without deadlocking, while preventing access by other threads. (Note that this implies that a recursive lock will not protect a critical section from a signal handler interrupting the thread holding the lock.) Here is an example where a recursive lock functions properly but other lock types would deadlock:
// create the lock only once!
NSRecursiveLock *theLock = [NSRecursiveLock new];
/* ...other code... */
/* ... possibly a long time of fussing with global data... */
[theLock lock]; /* possibly invoked in a subroutine */
The NSConditionLock, NSLock, and NSRecursiveLock classes all implement the NSLocking protocol with various features and performance characteristics; see the other class descriptions for more information.
Acquiring a Lock