- Inherits from:
- NSObject
- Conforms to:
- NSLocking
- NSObject (NSObject)
Declared in:
- Foundation/NSLock.h
NSRecursiveLock defines a lock that may be acquired multiple times by the same thread without causing a deadlock, a situation where a thread is permanently blocked waiting for itself to relinquish a lock. While the locking thread has one or more locks, all other threads are prevented from accessing the code protected by the lock. Here's an example where a recursive lock functions properly but other lock types would deadlock:
NSRecursiveLock *theLock = [[NSRecursiveLock alloc] init]; ... [theLock lock]; /* lengthy operations involving global data */ [theLock lock]; /* possibly invoked in a subroutine */ ... [theLock unlock]; /* relinquishes most recent lock */ ... [theLock unlock]; /* relinquishes the first lock */
Unless theLock was an NSRecursiveLock, a deadlock condition would occur at the second lock message in the example above.
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.
NSLocking
- - lock
- - unlock
- Acquiring a lock
- - lockBeforeDate:
- - tryLock
- (BOOL)lockBeforeDate:(NSDate
*)limit
- (BOOL)tryLock